What is the best way to detect if a player is in an enemy’s range of attack. For example, I have a turrets that has the top part rotating, and when a player is within it’s attack range, it should target the player. I’m specifically doing this for a multiplayer game. Since I can’t keep cycling through all players positions and check to see if they are within a certain range, I attempted to do a Physics.OverlapSphere in a big radius around my turret every 2 seconds. Everytime that happens though, the framerate drops 15fps, so I can tell it’s heavy on the cpu. What is the proper way of achieving this?
use triggers, triggers are optimized so there are fast, and they can easily detect anything that touches the trigger and not only the player
simply put a collider with the wanted radius and set it to be a trigger, then create the trigger handler that check if the object is in fact the player and if so, do whatever you want with it
Awesome, thanks!
use the vector3 distance, or raycast!
with Vector3.Distance you can set a range distance, if player is close then you got it, else you can use also raycast, but aren’t good stuff, it’ s better to use Vector3.Distance, and Vector3.Dot