Most efficient player detection method

I am currently about to program an attacking NPC. I plan on having many enemies in the game at once, so I want to know what the most performant way to detect a nearby player is. The methods I have though of are -

  1. Raycasting
  2. Region3
  3. Touched (I would weld a big box on the npc)
  4. Magnitude check

Which of these are most performant or is there a better option?

Region3 or Raycasting would be the way to go.

You could also try out the custom module Zone Plus, since it has some pretty good touch detection methods

1 Like

Thank you so much. I think I’ll try raycasting so I can easily add a line of sight feature to make sure the player isn’t behind a wall.

Yeah, I think you should combine raycasting and Region3 to make the most precise and efficient player detection method.

1 Like

i have found from experience that magnitude checks are best due to the fact that when spatial quarry functions pick up a lot of parts it can drain performance

2 Likes

I suggest zone plus, it’s extremely easy to set up and works if you want a box shaped zone

1 Like

Region3 & Raycasting along with magnitude checking is the way to go. You can try spatial queries such as workspace:GetPartsInBox but they come with a high memory cost, they are fast in lua though!

Use region3 to check the NPC’s surroundings while the raycasting is to check if the player is behind a wall. You may use dot product to see if the player is in the NPC’s FOV.

1 Like

Would this be very laggy if it were run frequently. For example in a heartbeat event?

I might look into that. If I wanted a range for detection and a range for attacking, would I use two different zones, or should I add players to a table who are in the first zone then run magnitude checks?

eh possibly a tiny bit if you have 100+ npcs, but if you run magnitude checks once per sec while the npc is idle then you should be fine

1 Like

have a distance check then use raycasting to see if the npc can actually see them

1 Like

You shouldn’t need to run this in a hearbeat event, even if the enemies are checking 3 times a second, that is still plenty fast for them to detect.

2 Likes

You could do both. I personally would do two different zones but I don’t think that makes much of a diffrence

1 Like