How to stop Area or Raycast from detecting body that is no longer in the area

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

I have an enemy that chases the player on sight (Used an area2D at first, then a Raycast2D thinking force_raycast_update would fix this)

The player is generally chased in a hallway that has doors along the background. These doors can be used with a key press to enter the respective room. Each room however is actually just somewhere off screen and the player character gets teleported to it when they use to the door.

The creature doesn’t strictly speaking follow the player but instead updates a last_known_player_position variable each time the raycast detects the player.

However when the player teleports away using a door the creature attempts to go to the location they teleported to, not the doorway they used, this is despite the fact that the last_known_player_position only updates when the raycast2D pings the player’s body.

This seems to mean that somehow the raycast2D is still detecting the player’s presence even after they are physically moved our of scanner range. Is this intended behavior or are areas and raycasts bugged currently?

I could fix this by just adding a “just used door” variable or something to the player character and forcing the creature to check that before updating last_known_player_position, but its very hacky and I’d rather understand what the physics system is doing to avoid problems in the future.

it could be that you teleport the player the same frame the lastknownplayaer_position is updated. If so, then the physics engine may not had the time to perform a physics update, so the raycast reports its still colliding with the player.

What if you add like single-frame teleport delay frame that prevents the lastknownplayaer_position variable from being updated the frame the player teleports and only re-enable the collision detection after a frame went buy after the teleport?

godot_dev_ | 2023-06-09 23:17

Putting a manual stopper in when the player character teleports is working, though I still don’t quite get why the physics engine works like this. The updating of last_known_position is done right after a successful ping, so even if the player moves with the ray still having the player’s position, it shouldn’t be pulling their post-teleport position.

It might be that the order things are done in terms of physics is more ambiguous than it immediately seems? I guess it’s just something people have to work around for now.

Hastings | 2023-06-10 00:17

Yeah, if you move an object outside a collision area, the engine will only be aware of this the next frame since it has to compute a physics step to determine collisions

godot_dev_ | 2023-06-10 02:27