Is using Area2D the only way of detecting collisions between objects?

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

First thing I want to say is that I’m new to Godot (and game development), so I may be doing a flagrant error.

I was trying to make a simple code to detect a collision between two objects (a character and a coin). I discovered that get_slide_collision only works if the opposite objec is an StaticObject. I wanted something more generic.

After googling for a while, I only found people saying that I should use an Area2D. I’ve used it and it works, but it has issues.

First, I have to duplicate the CollisionShape. Both KinematicObject2D and Area2D need a CollisionShape child. I didn’t figure out how to share the shape between the two. The code works, but duplicating data is wrong.
Second, when the game starts up, the Area2D detects a collision with the player, and triggers it’s signal. So the player detects a collision with itself. It doesn’t have any effect as the player isn’t in the “coins” group and it doesn’t further execute anything, but it shouldn’t happen. An object detecting a collision with itself is also wrong.

So my question is, Is there a cleaner way to detect a collision between a KinematicObject and a non-static object?

Thanks in advance.

:bust_in_silhouette: Reply From: kidscancode

When you were told to use an Area2D, they meant for the coin object.

Player: KinematicBody2D + CollisionShape2D
Coin: Area2D + CollisionShape2D

When the player touches the coin, the coin’s body_entered signal will fire. At that point you can do whatever you want to do - remove the coin, add points, etc.

You seem to lack some basic understanding of how the engine works. I highly recommend going through the Step by step section of the docs. Much of these basics are explained well there.

agreed the step by step is the best place to go for this. however if you find it TLTR the part of the step to step that will assist you is the Your First Game section teachs collisions and a few other basics you should be able to understand however a lot of it may go over your head if you haven’t read the prior text, for instance there are parts on the previous parts of the step by step which explain signals which are important for what you are trying to achieve.

Nofool | 2019-08-04 16:49