- Home /
Do OnTrigger functions have to check all objects in the trigger per frame?
I want to create a boundary system in my game, but I'm developing for mobile so I want to be as efficient as possible. A little background on what I'm trying to do: I have a scene with a ball that can be shot and followed. if the ball leaves the box trigger for that particular scene, then the ball stops being followed and is re-spawned. There are many other objects in this playable area however.
Two ways I thought of doing this was using OnTriggerEnter with 4 walls, or an OnTriggerExit with one square box. pseudo code below:
//pseudo
//4 walls with this function
void OnTriggerEnter(collider other){
if(other.tag == "foo"){
//call respawn function
}
}
//or do it this way with one box:
void OnTriggerExit(collider other){
if(other.tag =="foo"){
//call respawn function
}
}
I know these work as intended for their purpose, but in the case of Exit, does the collider field other require all objects in that trigger space to be checked each frame? If so, is it possible to "Filter" the objects so its only checking for the ball each frame? (would i use a custom layer?)
a parametric boundary system would potentially work as well if someone could point me to a source :)
Answer by tanoshimi · Jun 18, 2017 at 04:33 PM
You should use layer-based collision to only check collisions against objects in specified layers.