Efficient way to trigger something when player is near

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

So I want my enemies to react when a player is near, either change animation, attack etc. With multiple enemies active at the same time, I’m wondering what’s more efficient:

  1. In each enemy’s code find player node and calculate distance every frame or
  2. Add an area to each enemy and use the on area entered signal/function or
  3. Some other way?

I’d just like to know what’s more efficient in Godot and when does it start to matter, as in how many objects like this would there have to be in the scene for it to start significantly affecting performance.

The Area nodes could be used to detect the player, and have the enemies react to the player’s movements. The enemies could also have field of views which can detect the player. There’s an example of this in the Vector math article on the dot product.

Ertain | 2020-05-23 23:16

:bust_in_silhouette: Reply From: RazorSh4rk
  1. is probably easier and the recommended way. Shouldn’t affect your performance too much, i put down 1000 objects to test it and the fps didn’t even drop from 60.