Newb here and I trying to determine “best” pattern for a NPC to detect a player.
I’m currently using raycasting to have the NPC basically look for a player, but I also want the NPC to be able to maybe “hear” a player behind them. I thought about putting an invisible part around the NPC and detect collision, but that doesn’t feel right.
I see a lot of post that loop through all players to find ones “close” and that seems like a unnecessary loop. I would rather it work more organically and avoid looping unless performance wise that is best approach.
I assume this is a design pattern that has already been solved, but having trouble finding it.
There are 2 other options of detecting players behind/around the NPC.
You can create Region3s behind/around the NPC and use FindPartsInRegion3 to get the players in the Region3.
You can also create a hitbox part behind/around the NPC and use GetTouchingParts to get the players in the hitbox.
However:
Looping through players and computing the distance from them will be less expensive, as it’s just a magnitude comparison from the player’s HumanoidRootPart position to the NPC’s position. You can use Player:DistanceFromCharacter to reduce overhead from Vector3 arithmetic.
Region3s require more math operations to compute which parts are inside it, and the same goes with GetTouchingParts. Read the “How do Region3 checks work?” section on the FindPartsInRegion3 page.
So sounds like you are saying there is no need to try to get fancy with this and retrieving/looping through all the players is the best approach to detect players and just determine based on position diff if they are in line of site or behind ?
Personally, I would just loop through each player and compute the magnitude.
From my experience, depending on .Touched is terrible and inaccurate. Not a good idea.
For Region3 though, I can’t say anything about it because I’m not familiar with it.