*Solved* Disable Trigger for other Objects

Hey guys,

I have a GameObject with a SphereCollider as a trigger around it that should rotate towards another Object entering that trigger (I use OnTriggerStay here).

Now I want that trigger to ignore every other Object entering it as long as there is an Object staying in the trigger. I have abolutely no idea how to handle that…
In simple: If the trigger is triggered i don’t want it to be triggered by anything else but the object inside the trigger.

Tanks for your help.

well you can use OnTrigger enter to store the object, have an if statement that only changes it if it’s null,
in the OntriggerStay() check if the stored gameobject is the same as the one in the OntriggerStay, do the rotation.
in the OntriggerExit set the stoged gameobject to null

If you need some code I can post something too

1 Like
private Collider storedGameObject;


void OnTriggerEnter(Collider other)
    {
        if(storedGameObject == null)
        {
            storedGameObject = other;
        }
       
    }

void OnTriggerStay(Collider other)
    {
        if(storedGameObject == other)
        {
         //do your rotate code or what ever
        }
    }

void OnTriggerExit(Collider other)
    {
        if(storedGameObject == other)
        {
         storedGameObject = null;
        }
    }
1 Like

Thank you so much. I’m new to all this programming and sometimes I can’t find simple solutions like this.

So yes, it works in the way I need it.

I wont give you code cause i dont remember it lol but i can guide you. When the trigger is triggered, it stores the object that triggered the event. So its a matter of doing something like event.target, i dont remember the exact name but intellisense will help.
In the other hand if you want to deactivate the trigger, just get the component with a GetComponent or something in that line. if your guy that activated the trigger leaves, and while the collider is deactivated, you should use a distance method to calculate how far he is, for this u can use Vector3.distance. when its value is above lets say 5, you activate the collider again.