How Detect Collision within GDscript?? (2D)

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

I’m making a 2D RPG type thing, and I wondered if there was an easy way to detect if the player (CharacterBody2D) walks within a square but does not collide with it, because I was thinking of having an area that you have to stand in in order to open a door, so you can’t open it from across the room but don’t need to stand up against it. In Python I would normally just do if player_rect.colliderect(door_rect):. The door is a StaticBody2D and I was thinking I could use a CollisionShape2D but have no idea how to implement it in the script.

:bust_in_silhouette: Reply From: evpevdev

If you dont already have a CollisionShape2D as a child of your StaticBody2D and CharacterBody2d, add them now. Then, make sure you are moving your player using move_and_slide( ) function and then you can use get_slide_collision_count( ) and get_slide_collision( ) functions to detect if your player is colliding with something.

This tutorial in the documentation will get you started.

:bust_in_silhouette: Reply From: Cyber-Kun

alternatively you could add an Area2D node wich has a signal on_body_entered() for the object and just connect to the signal when the character is in the area it’ll send the signal and you can respond appropriately