Active a fonction by detect the superposition of two collisionshap2D

Godot-4

Can you help me don’t know how to active a fonction by detect the superposition of two collisionsshap2D can you give me a leçon or a exemple.

CollisionShape2Ds should only be used as children of CollisionObject2Ds.
For this case specifically, Area2Ds would probably be best (if you don’t need them to have physics).
The Area2D class provides signals for handling collisions with other Area2Ds or bodies:

  • area_entered(area: Area2D)
  • area_exited(area: Area2D)
  • body_entered(body: Node2D)
  • body_exited(body: Node2D)

To detect a collision between two Area2Ds, you can attach a script to one of them (or, theoretically, to any other object), for example:

extends Area2D

func _area_entered(area: Area2D) -> void:
	print("area ", area, " has entered the area")
func _area_exited(area: Area2D) -> void:
	print("area ", area, " has exited the area")

Then you would just need to connect the signals area_entered and area_exited to their corresponding functions in the node section of the inspector


Now, if an area with an attached collision shape enters or exits the area with the script, you should see a message in the console (which you could replace with actual logic)