I have been testing this extensively in my game, and please if i’m making some kind of mistake, please point it out.
The idea is, imagine 40 entitys, all navigating in the map. Each entity has 3 triggers, 1 for the left, 1 for the right, 1 for forward.
Each trigger has a mesh, and a rigid body with kinetic enabled.
Then each entity has a simple mesh colider (no rigig body), and a trigger on it.
The idea is, the triggers of each entity will detect if there’s a entity near it, and will avoid it depending on what trigger is on.
Ok, all of this is working, but, once the triggers overlap, my frame rate drops from 150fps to 2fps.
So, I ended up putting all triggers in a specific tag, and at startup, i go trough each trigger and I make a Physics.Ignore between all of them.
And ok, now it’s faster, but still drops a lot.
I just remade all of this code with RayCasting, and well to my surprise the outcome is 400% faster or more.
Is there a performance hit using triggers ? Is it because they need to have a rigid body attached ?
The problem may be to do with the number of mesh colliders. These operate much more slowly than primitive colliders for most tasks. If you replace as many mesh colliders as possible with primitives, you are likely to get a performance boost.
I’m using triggers for my projectile colliders (which are sphere colliders) and they don’t have a big impact on the performence. Due to the nature of the game, sometimes 20-30 triggers are registered and this runs great even on iPhone.
Thanks for your experience Mortis.
In my game, if I use 30 triggers, I will go down to 5 fps or low…, and you have that in the Iphone ?
I have a feeling i’m doing something terribly wrong here.
You do have a rigid body attached to the trigger right ?
Yes Bruno, they all have rigidbody components otherwise the projectile wouldn’t trigger the enemy colliders. Are you sure that when you remove the triggers, your game runs without problem?
Maybe the problem is what you ask your game to do when trigger happens. I would suggest you to just write a debug message using Debug.Log(“Triggered”) when trigger happens and see how that works. It is really hard to understand why do you experience such a performence loss without seeing your code and even project setup.
Yeah, I have rigid bodys too in there, and I was thinking that was the reason it was so slow.
I’m pretty sure that without the triggers runs without a problem yes, it’s how I have it now, I was tryng triggers to get maybe a boost in speed, however as I explained, it happens exactly the opposite.
I have a OnTriggerStay, and inside it, the only thing I do is setting a variable to 1 , that’s all.
variable.on = 1 … pretty much that.
I don’t get it.
What could be in the project setup that could cause this ?
What happens when you set that variable to 1? Please try removing that line and test it with a normal print(“Triggered”); or Debug.Log(“Triggered”); command just to make sure it gets triggered and if the performence will receive any impact.
And I’m pretty sure as you are using OnTriggerStay, whatever happens in that function happens every frame. And if you have 20 triggers it happens 20 times a frame and depending on what you are doing, it is an overkill. Please explain what you want to achieve and maybe we can work out another way.
I know whatever happens in OnTriggerStay happens every frame, that’s exactly what i want.
But ok, imagine one ship, this ship has a body and 2 wings.
Right now i’m casting (every frame) 3 rays, 2 from each wing, and one from the body, this raycasts go forward with a range of around 100 Unity units.
When i finds something, i simply register the impact point,and i find a way to turn around.
Let’s imagine that when the ray hits something, calls a function called “HIT”, and this function is only executed if a control variable is set to zero.
With 100 ships on the screen, i get above 100 fps with this.
Now, i simply remove the raycasting, and i replace the raycasting with a cube, that i place in front of the ships. 3 cubes, 1 for the hull, and 2 cubes for the wings. When one of this cubes is triggered, i call the function “HIT” that was being called before when i get a hit with the raycastings.
With the triggers, i get above 100 fps (as long as nothing is being triggered). Once i get triggers of the ships over eachother, my FPS drops to 5 or lower.
I don’t think this is normal, either that, or something is very wrong here. Triggers should be much faster than raycasting.
In the profiler, i get waaaay to much time used in physics using the triggers, and from this came my conclusion that the problem was the rigidbodys that i need to place in the triggers itself.
I have a mesh collider in the ship (no rigid body, just a mesh collider with the trigger enabled), and for the triggers, i have a mesh collider with the trigger enabled and a rigid body).
Do you see anything wrong here ?
The first thing comes to my mind is the utterly slow mesh colliders. You should try to replace them with compound colliders (Colliders that are a combination of normal primitive colliders like sphere, capsule etc.).