Any way to create a collision shape (3d) from code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gokudomatic2
:warning: Old Version Published before Godot 3 was released.

I see the question was asked a couple of times for 2d and it seems there are some solutions for that. But how to do it in 3d?

First I thought that a mere CollisionShape.new() and a set_shape to it with an add_child to my kinematicbody was working, but I noticed that when the scene had before a collisionshape created in the editor and then it was deleted, the collisionshape is still used, leading me to the false idea that my code was working.

I tried to use KinematicBody.add_shape() but Godot is crashing right away when I play the scene.

:bust_in_silhouette: Reply From: Zylann

It’s the same API in 3D. Shapes used for collision are not nodes, you don’t need to add a child in any way (which is only an editor convenience) or creating CollisionShape either. Adding a shape to a physical object from a script is done with add_shape and related methods, and the shapes you can add are different classes: http://docs.godotengine.org/en/stable/classes/class_shape.html#class-shape

If Godot crashes (error? or real crash?) when you use add_shape, it’s probably a bug that you can report on Github if you have a minimal project that shows it. Or maybe you tried to add a shape that actually was a CollisionShape node, and you ran into Godot not understanding what was going on.

You were right. Thanks for the fast answer. Indeed my mistake was to give a collisionshape node in add_shape. Strange enough, Godot doesn’t give any error message. It only ends the game right away.

Now I have to figure out how to set the translation of my shape. Because I have 2 shapes to create and they are at a specific place, which I must even move later in the game. And those shapes objects have no transform field. I’ll probably have to ask a new question for that new problem.

Gokudomatic2 | 2017-01-26 09:44