How add collision shape to a multimeshinstance ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DavidAvila

Hello everybody, quick question, is there a way to add collision shapes to a multimeshinstance?. Couse i’ve being messing around with it and when the player collide whit the meshes it does’nt stop and keep walking. Both the Target Surface and the Source mesh have there own collision shape node inside a staticbody node. Thanks

:bust_in_silhouette: Reply From: klaas

Hi,
a quick google search brings up this …
https://forum.godotengine.org/52924/solved-how-set-collisionshape-on-multimeshinstance

Thanks a lot, i will try it.

DavidAvila | 2020-09-21 00:43

:bust_in_silhouette: Reply From: DavidAvila

Hello klaas, after a lot of testing and code changes, i found the solution (at least in my opinion).
And it’s easier than i thought. So, we got a ‘multimeshinstance’ with the Target surface and the Source mesh selected and all other options of the multimesh as nedded, and then we attach the following script to the Multimeshinstance Node:

extend MultiMeshInstance

func _ready():
  var multiMesh = get_node("."). multimesh
  
  for index in multiMesh.instance_count:
       var meshTran = multiMesh.get_instance_transform(index)
       var shape = Collision_shape.new()
       shape.shape = multiMesh.mesh.create_trimesh_shape()

       shape.transform.basis.x = meshTran.basis.x
       shape.transform.basis.z = meshTran.basis.z
       shape.transform.basis.y = meshTran.basis.y
       shape.scale = Vector3(1,1,1)

       shape.rotation.y = 25.132
       shape.rotation.x = 25.132
       shape.rotation.z = 25.132

       var sBody = Staticbody.new()
       sBody.transform = meshTran
       sBody.add_child(shape)
       add_child(sBody)

       index += 1

Hey man this code doesn’t work, after switching extend to extends, it can’t find Collision_shape.new, if I switch that out with an existing mesh name it throws an error stating that it’s outside the scope. Any thoughts on this? I know it’s old.

Good-Boi McLovin | 2021-02-17 05:06

Nevermind got it working, there were a few misspellings in your script, thank you a lot for this, super useful, tho I wish the scale could be applied based on the instance’s own scale parameters but I did not see a way to do so.
The fixes made are minor, but I’ll list them here if anyone needs them:
line 1: extends not extend
line 9: it’s CollisionShape.new() not Collision_Shape

I also added my own stuff, because while it’s amazing to be able to use the instanced mesh as the source for creating the trimesh collision shape, doing so with lots of fairly complex objects (trees in my case) can be I assume costfull, so instead, I implemented a cylinder as the collision, in roughly the same dimensions, they extend higher into the air than normal so it looks like your colliding with the tree when it’s actually a more simple object.

extends MultiMeshInstance

func _ready():
	var multiMesh = get_node("."). multimesh
	#var Collision_shape = CylinderShape.new()
	
	for index in multiMesh.instance_count:
		var meshTran = multiMesh.get_instance_transform(index)
		#var shape = CollisionShape.new()
		var shape = CollisionShape.new()
		var shapeMesh = CylinderMesh.new()

		#shape.shape = multiMesh.mesh.create_trimesh_shape()
		shape.shape = shapeMesh.create_trimesh_shape()

		shape.transform.basis.x = meshTran.basis.x
		shape.transform.basis.z = meshTran.basis.z
		shape.transform.basis.y = meshTran.basis.y
		shape.scale = Vector3(1,6,1)
		
		shape.rotation.y = 25.132
		shape.rotation.x = 25.132
		shape.rotation.z = 25.132

		var sBody = StaticBody.new()
		sBody.transform = meshTran
		sBody.add_child(shape)
		add_child(sBody)

		index += 1

Good-Boi McLovin | 2021-02-17 07:16