When having a MeshInstance3D you can “Create Trimesh Collision Sibling” in the Editor, which creates a CollisionShape3D but i cant recreate it by code.
There is the create_trimesh_collision() Method, which creates a StaticBody3D with a child node CollisionShape3D, which not behave like the editor version of it.
parent_node.create_trimesh_collision()
var staticbody = parent_node.get_child(0)
var collisionshape = staticbody.get_child(0)
staticbody.reparent(parent_node,false)
#collisionshape.transform.origin = parent_node.transform.origin
What i want is the collisionshape, which i need to reparent and have its position, which it should have by my settings. Last line is just what i tried.
Im not sure what to do with this, i tried to assign the output of create_trimesh_shape() to a CollisionShape but nothing changed to that what i tried in the code above
If i print out the editor version of mesh and the shape of new created CollisionShape3D from this approach i get different id’s
Mesh::create_trimesh_shape() creates a new collision shape so the object id will always be different.
There are not more steps to the procedural than:
var instance: MeshInstance3D = # wherever your node is
var cshape: CollisionShape3D = CollisionShape3D.new()
cshape.shape = instance.mesh.create_trimesh_shape()
var body: StaticBody3D = StaticBody3D.new()
body.add_child(cshape)
instance.add_child(body)
Use simple box / sphere collision shapes for your bone attachments and only if you have to capsule shapes.
Trimesh is the worst physic shape that you can pick for this for both performance and quality reasons. I know everyone wants “perfect” mesh collision but that is not how those things are cooked in games especially not when animated.
Depending on your games frame rate and how quick your animated movement is you might need to set your animation processing to physics else your visuals and your collision will not match up at all bone attachment or not.