How can i add a custom collision shape to a StaticBody2D node?

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

i want to draw custom shapes use them as platforms in my game.
i tried using a StaticBody2D node for this, and i had no problem getting it to draw the shapes, but i got stuck on setting the collision shape, all the threads i could find suggested using a Polygon2D with the shape i want and adding it using the “add_shape” function,
but i get this error:

Invalid call. Nonexistent function ‘add_shape’ in base ‘StaticBody2D (’.

what am i doing wrong? should i use a different base?
thanks a lot, im a newcomer just started today :slight_smile:

:bust_in_silhouette: Reply From: kidscancode

add_shape() is not a function of StaticBody2D.

CollisionObject2D nodes (static/rigid/kinematic) have an api for adding shapes:

but it can be complex to use, as you need to create a shape owner and then add the shape, etc.

It’s much more straightforward if you use CollisionPolygon2D:

Add it as a child of your StaticBody2D and set the polygon property using the same point array you used for your Polygon2D:

$CollisionPolygon2D.polygon = $Polygon2D.polygon

Bless you mate, may i ask what is the meaning of the dollar signs you typed in the code?

danny57 | 2018-06-05 19:50

The $ in GDScript is a shorthand for get_node(). So it means the same to write




kidscancode | 2018-06-05 19:55