- Home /
Least heavy processing detection method
Hey all! I have used Unity for nearly 4 years now and I really do love it. Best engine I have used.
Jumping straight to it, what is the best method for detecting items in game?
Example scene: Movable player, NPC on a Nav Nesh.
What's the least processor heavy, most accurate (as in detecting the player at a range AND from any direction), shortest code (without bunching it all together) and / or easiest to manipulate to multiple creatures (as in changing speed, how fast it notices you etc..).
The examples in which I know about all ready are:
1) Shooting Raycasts from the NPC to detect the player.
Advantage: Can control how fast it checks the cast meaning less processing, fairly short code, easy to add to multiple creatures.
Disadvantage: Can only fire in certain directions, I.E forward, back etc... Meaning not very accurate. (Could be fixed focusing on the player only but would ruin anything to do with enemy NPC)
2) Adding a trigger to the character / NPC and checking in OnTriggerEnter and OnTriggerLeave for the other one to enter and leave.
Advantage: Fairly short code, easy to add to multiple creatures, fires in every direction.
Disadvantage: Can't control how fast it checks.
3) Vector distance I.E taking player position from NPC position then using magnitude
Advantage: Easy to add to multiple creatures, fires in all directions and can control how fast it checks.
Disadvantage: Lots and lots of if statements and long code.
Is there any other methods? If not which of the three should be used when many NPC's are spawned?
I don't want anyone to write the exact code. Just the best method so I can code it myself.
Thank you for any assistance!
Answer by MakeCodeNow · May 15, 2014 at 01:11 AM
As with a lot of questions of this type, the answer is it depends. In particular, it's not clear if you are asking about a many-to-one (i.e. each NPC checking for the player) or a one-to-many (the player checking for all NPCs in range). It also depends on if you need to do the check every frame or if you can afford to check every-so-often.
In the one-to-many case (player checking for NPCs) a trigger is almost certainly the way to go, because internally it's taking advantage of all the sexy and fast spatial partitioning in PhysX. Also, players usually need to do checks like this every frame, which the trigger will do anyway.
In the many-to-one case, a distance squared check is both simplest and fastest. I'm not sure why you said that the distance case requires more checks than any other. As long as you are comparing apples to apples (i.e. from many or from one) then it should be the same number of checks. Usually, I see NPC vision code written like this. You can cut out internal terms if you want to gain speed at the cost of accuracy:
// Quickly written untested code.
Vector3 vecToPlayer = player.position - transform.position;
float squaredDistance = Vector3.SqrMagnitude(vecToPlayer);
if(squaredDistance < (sightRange * sightRange) // square distance check
{
vecToPlayer /= Mathf.Sqrt(squaredsitance); // normalize the vector
if(Vector3.Dot(vecToPlayer, transform.forward) > sightCone) // view cone check
{
if(!Raycast.hit(transform.position, player.position)) // raycast check
{
// we saw the player!
}
}
}
Thank you for explaining both methods! How did you find out that triggers are better or is it just common knowledge?
Common knowledge of how spatial queries generally, and physics engines specifically, work.