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 -
Raycasting
Region3
Touched (I would weld a big box on the npc)
Magnitude check
Which of these are most performant or is there a better option?
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
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.
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?