- Home /
Collider collision processing speed
Hi, I'm creating a stealth game at the moment.
The sequence for Player Detection is
if(PlayerInRange())
{
TurnOnCollision();
}
OnTriggerEnter()
{
CheckIfPlayerHitCollider();
}
I was worrying that the processing will be slowed down if the collider will be ON all the time since it will hit many collider objects beside Player. Thus, I made it so that the Detection mesh collider will be activated only if the player came within certain range.
So, I was wondering on how heavy the OnTrigger-related functions are.
Answer by Aaron Winterhoff · Apr 20, 2013 at 03:01 PM
This requires a bit more information.
Does TurnOnCollision therefore turn on the collider itself?
Is the "DetectionMeshCollider" a Unity sphere collider, or a mesh that you've made? Mesh colliders are generally more intensive than one of Unity's built-in colliders (sphere,box, capsule etc).
That said, if you're using a unity collider, unless you have a huge amount of enemies searching for an enemy, you should be fine.
These things take place in the background of unity, and are ultra-fast physics calculations. The call OnTriggerEnter () gets called whenever something Enters the collider, and if you have a OnTriggerEnter () call, then it's going to be doing the check anyway. Long story short, Unity is faster than we are.
If you look at their documentation itself, you can see that they ( point 5 ) actually refer to your exact problem. Their solution is to use Triggers, not check a distance. If you're checking a distance as well, then you're just doubling up on Unity's workload.
Yes, TurnOnCollision() was suppose to turn on the Collider.
Answer by Jazzer008 · Apr 20, 2013 at 02:47 PM
Personally, I'm not a big fan of triggers. It might be the way I'm using them, but sometimes they don't seem to function without issues.
I think it would be less 'heavy', to grab the positions of each objects and check the distance between them.
if(Vector3.Distance(object1.transform.position,object2.transform.position) <= minimumDistance){
TurnOnCollision();
}