- Home /
how to cancel OnTriggerExit when already hitting a 2nd trigger
my issue an object moving when it hits the "floor" it triggers a flag variable, "not_on_floor", to get set to 0 and then when it stops touching the "floor" i change the flag back to 1. The flag variable is then used in the update function to cancel out and adjust movements while it is is touching the "floor".
the problem is when there are two objects present with the tag "floor" that are within close proximity. The object stops touching the first "floor" while it has already begun touching the second "floor". Upon exiting the first "floor" the flag gets momentarily changed changed to 1 before the OntriggerStay resulting from the second "floor" can push it back to 0.
this results in the movement basically skipping a beat.
what i would like is for the not_on_floor flag to stay 0 until it is not touching any of the triggers tagged with "floor"
the object is a kinematic ridgedbody with a box collider.
below is the code controlling the flag.
function OnTriggerStay(other: Collider){
if (other.tag == "floor"){
not_on_floor=0;
}
}
function OnTriggerExit(other: Collider){
if (other.tag == "floor"){
not_on_floor=1;
}
}
Answer by aldonaletto · Jul 26, 2013 at 05:13 AM
You could use OnTriggerEnter/Exit and count the events:
private var qTrigger = 0; // count OnTriggerEnter
function OnTriggerEnter(other: Collider){
if (other.tag == "floor"){
qTrigger++;
not_on_floor = 0;
}
}
function OnTriggerExit(other: Collider){
if (other.tag == "floor"){
qTrigger--;
if (qTrigger == 0){ // this was the last trigger
not_on_floor = 1;
}
}
}
}
Thanks aldonaletto worked great. i was using enter the first time and tried to use the stay to hold but that just made it not work in another way. The counting works perfect and a simple fix. Thanks a lot!! i just started learning unity on my own so it was a big help actually and gave me more insight on how to structure. I had a feeling that i was wasting resource with the stay.
Answer by usernameHed · Mar 25, 2019 at 07:42 PM
The Answer of aldonaletto works great, but it's not enought if you have multiple object. Here is my suggestion:
using System;
using System.Collections.Generic;
using UnityEngine;
public class GravityAttractorTrigger : MonoBehaviour
{
[Serializable]
public struct TriggerCountExit
{
public GameObject objectTriggered;
public int count;
public TriggerCountExit(GameObject obj, int _count)
{
objectTriggered = obj;
count = _count;
}
}
[SerializeField, Tooltip("enum to interact")]
private List<string> tagList = new List<string>()
{
“Player”,
“Enemy”,
“Object”
};
[SerializeField, Tooltip("enum to interact")]
private List<TriggerCountExit> countCancelExit = new List<TriggerCountExit>();
private int ContainObject(GameObject obj)
{
for (int i = 0; i < countCancelExit.Count; i++)
{
if (obj.GetInstanceID() == countCancelExit[i].objectTriggered.GetInstanceID())
return (i);
}
return (-1);
}
private bool ManagerEnterCancel(GameObject other)
{
int contain = ContainObject(other);
if (contain != -1)
{
TriggerCountExit newTrigger = countCancelExit[contain];
newTrigger.count++;
countCancelExit[contain] = newTrigger;
return (false);
}
else
{
countCancelExit.Add(new TriggerCountExit(other, 0));
return (true);
}
}
private bool ManageExitCancel(GameObject other)
{
int contain = ContainObject(other);
if (contain != -1)
{
TriggerCountExit newTrigger = countCancelExit[contain];
newTrigger.count--;
if (newTrigger.count < 0)
newTrigger.count = 0;
countCancelExit[contain] = newTrigger;
return (countCancelExit[contain].count == 0);
}
else
{
//error, can't be
return (false);
}
}
private void OnTriggerEnter(Collider other)
{
if (tagList.Contain(other.tag))
{
If (ManagerEnterCancel(other.gameObject))
{
//here action enter for the first time
}
else
{
//here action for the second, third time…
}
}
}
private void OnTriggerExit(Collider other)
{
if (tagList.Contain(other.tag))
{
if (ManageExitCancel(other.gameObject))
{
//here you can do your exit action
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
checking to see if multiple (different) tags are destroyed 2 Answers
Multiple triggers on one object 8 Answers
Child Object's collider setting off triggers 1 Answer
multiple enemies does nt work with this script but a a single enemy works..Please help 0 Answers
Why does the money counter resets when I pull different objects trough the trigger? 1 Answer