How to create a collision shape with a script on Godot 3? This worked in Godot 2.1...

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

I am trying to dynamically create platforms, along with their collision shapes.
I actually managed to do it in Godot 2.1, but in Godot 3 I get an error because “add_shape” does not exist anymore.

Any ideas how to achieve the same? This is the snippet of code that worked in Godot 2.1:

	# add collision shape of size PLATFORMSIZE	
var shape =


Now “add_child” gives an error. I am just trying to add a collision shape to new_platform and then add new_platform to my world.

:bust_in_silhouette: Reply From: kidscancode

You have two options. If you add a CollisionShape2D child, you can add your new shape to it. Otherwise, you have to use the collision/shape_owner API, which I think is more cumbersome to use.


var collision =
var shape =
shape.extents = Vector2(5 * PlatformSize, 1))
collision.shape = shape

See CollisionObject2D docs for details of the shape_owner API. You might also find it easier if you create a default platform scene that includes a collision shape and instance that, so that you only have to set the shape extents.

You didn’t mention what error add_child() was giving you, so I’m not sure what to advise about that.

Thanks a lot.
That helped immensely!

aleks | 2018-07-09 18:47