3
votes

I have a Rigidbody2D node (which requires CollisionShape2D) and inside it I have an Area2D node (which requires CollisionShape2D as well). When I create 2 of those CollisionShapes, it breaks the physics. So I would like to have only one CollisionShape, but working for both physics and triggering actions when something enters it. Is it possible?

Maybe my approach is absolutely wrong, I'm pretty new to Godot, and if so, please tell me how to do this the correct way. Thanks!

2

2 Answers

1
votes

There is a gotcha, but from what you wrote I'm not sure you set up everything properly, so let's start at the begining.

Your tree structure should look like that: enter image description here The big outer blue square is my Area2D's CollisionShape2D shape, while the small inner blue square is my RigidBody2D's CollisionShape2Dshape.

Your Area2D should have a signal set up properly (i.e. connected to a script somewhere in your scene tree). For this example I chose to put my script on the RigidBody2D itself and have my signal caught here. enter image description here As you can see, I decided to catch any body entering my area.

Now, if you look at my code, here is the gotcha: you have to ignore your Area2D's parent RigidBody2D itself, otherwise it will keep detecting the collision with its own parent.

Of course, the best way of doing that kind of thing is by making use of collision layers and masks and not checking every time if the collision comes from the parent or not.

1
votes

Collision layers and masks are the way to go. Go into Project Settings and name your collision layers first under General (tab) and Layer Names (left side at the very bottom). Then you can find your layers and masks as rows of little boxes under the header PhysicsBody > Collision. On the right is a button with a couple of dots, it shows the named layers with checkboxes.

Layers are for categorizing everything. Masks are for defining which layers each object will collide with. You can read more here: http://kidscancode.org/blog/2018/02/godot3_kinematic2d/