- Home /
Performance: How expensive are Trigger Collisions? How many collision layers should be used?
Hi everyone,
I'm currently working on an RTS Game. All movable objects have a rigidbody and several trigger colliders to detect other objects or for example check whether an object is in range or not. I use gameobjects with different layers for each type of object (units, animals, zombies, vehicles, static objects, etc.) and each type of trigger detection I need for that object type (touch trigger, near trigger, in sight trigger, etc.) so I can set the collision matrix to only detect collisions when needed and this way hopefully enhance performance. But since there are only 32 layers available and there are a lot of different objects that need different trigger colliders in my propject, I'll soon run out of layers, since I need them for other things as well. So my question is, how expensive is trigger collision? Does it make sense to avoid unnecessary collisions with different layers and the collision matrix in my case? Or should I let the trigger collisions be detected and analyze them with different tags or IDs to check what kind of trigger and what kind of parent object collided? And how well would this perform in comparison? Does it even make a difference?
Thanks in advance :)
Answer by SteenPetersen · Jun 06, 2020 at 09:40 AM
Colliders, in general, can be imagined to be kind of the same as shooting a ray in all directions. so sometimes a collider, in this case, a trigger is necessary if it is absolutely impossible to know where collisions are coming from. If you know which layer the collision is coming from then you should definitely use a collision matrix to avoid having to check all objects, but instead, limit checks to only the object on the relevant layers.
Therefore, if you can, try and use raycasts instead, in places where that is possible as they are MUCH more performant than colliders. Colliders should be considered as your last resort.
A raycast with a layermask is as performant as you can get and is not even comparable to using a collider for the same task.
Your answer
Follow this Question
Related Questions
OnCollisionExit2D doesn't activate after layer change? 0 Answers
Collision Matrix not working as intended 1 Answer
Does the "Layer Collision Matrix" affect triggers? 1 Answer
using IsTouchingLayer without collision 0 Answers
Trigger Colliders: adding/removing rigidbody/collider at runtime is unreliable or confusing 1 Answer