- Home /
Inverse of OnTriggerStay?
Hey guys! All is in the question: is there a way to do the inversion of OnTriggerStay? Like a function that will return something if there's absolutly NO objects on trigger?
Thanks
Answer by Kiwasi · Jan 19, 2015 at 04:52 AM
There are a couple of options. If the collider is a sphere you can simply use OverlapShere to check for the presence of any colliders.
The other way is to keep track of all of the present colliders by adding them to a collection with OnTriggerEnter and removing in OnTriggerExit.
Alternatively you can also use OnTriggerStay together with the Update to keep track of if there has been any collisions:
private bool inTrigger = false;
private void Update()
{
if(!inTrigger)
{
//No objects in trigger
}
inTrigger = false;
}
private void OnTriggerStay()
{
inTrigger = true;
}
The problem here is that the inTrigger variable will always switch between true and false because the Update function will always turn it back to false...
Can you show me an example of keeping track of the colliders? I tried working with arrays but it hust didn't works
List<GameObject> myObjects = new List<GameObject>();
void OnTriggerEnter (Collider other){
myObjects.Add(other.gameObject);
}
void OnTriggerExit (Collider other){
myObjects.Remove(other.gameObject);
}