0 votes

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

in Engine by (16 points)
edited by

2 Answers

0 votes
by (3,130 points)

Thanks a lot, i will try it.

0 votes

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
by (16 points)

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.

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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.