All of them work, but the best one depends of what you want to do and mostly your preferences:
Area2D
For general collision detection i think is best option, you can use the body_entered
, body_exited
/ area_entered
, area_exited
signals to detect when the collision happens without need to check every physics frame, but if you want to check manually you can also use Area2D.get_overlapping_bodies()
/ Area2D.get_overlapping_areas()
.
RayCast2D
The RayCast2D
has two advantages compared to areas: You can get the position where the collision happened and you can do more than one check in the same physics frame. “What you’re means with check more than one time in the same physics frame?” In Godot, the collisions are checked only one time per physics frame, so as example, if you need to check a collision in one position and in the same frame you need to check other position, in this case you can repositionate the raycast and use RayCast2D.force_raycast_update()
to do a new physics test in the same frame. The negative side is that require more code than Area2D
and also is more computationally expensive.
ShapeCast2D
Has the same advantages and disvantages of the RayCast2D
, but is more expensive to use because check for more than a line.
If you’re using just one or two Ray/ShapeCasts
this will not destroy your perfomance, that is more important when you have hundreds or thousands of Ray/ShapeCasts
.