- Home /
OnTriggerStay Weirdness
This seems to keep calling OnTriggerStay, even long after the other collider has moved away from it. Any insight into why this is happening, and how to make it stop?
So a little context:
My game has a moving brush, tagged "PlayArea", which contains all the action, basically. The playarea uses a mesh collider, because the collider needs to be wedge-shaped for my purposes. This is supposed to determine whether the collider is in contact with the playarea's collider, and swaps objects for a placeholder object, depending whether or not they're within the playarea. It works just fine as far as activating objects, but not so much when it comes to deactivating them.
I'm trying to get around using an OnTriggerExit, because I want it to work independently of whether or not it started out within the playArea brush.
Here's the code:
#pragma strict
var activateDelay:float = 0;
var deactivateDelay:float = 0;
var activateOnStart:boolean;
var deactivateOnStart:boolean;
var activateOnPlayAreaEnter:boolean;
var deactivateOnPlayAreaEnter:boolean;
var deactivateOnPlayAreaExit:boolean;
var activateOnPlayAreaExit:boolean;
private var attachedObject:GameObject;
private var isInBounds : boolean = true;
private var busy : boolean = false;
function Start ()
{
attachedObject = transform.parent.gameObject;
if (activateOnStart && attachedObject.active == false)
{
//print (gameObject + " starting inactive, activating!");
Activate();
}
if (deactivateOnStart && attachedObject.active == true)
{
//print (gameObject + " starting active, deactivating!");
Deactivate();
}
}
function Update ()
{
}
function LateUpdate()
{
}
function FixedUpdate()
{
//print (gameObject + " FixedUpdate check isInbounds = " + isInBounds + "!");
if (isInBounds == false)
{
//print (gameObject + " DoOutOfBounds!");
DoOutOfBounds();
}
isInBounds = false;
}
function OnTriggerStay (col:Collider)
{
if (col.gameObject.tag == "PlayArea")
{
isInBounds = true;
DoInBounds();
//print (gameObject + " TriggerStay Check True!");
}
}
function OnCollisionStay (col:Collision)
{
if (col.gameObject.tag == "PlayArea")
{
isInBounds = true;
DoInBounds();
//print (gameObject + " CollisionStay Check True!");
}
}
function DoInBounds()
{
if ((activateOnPlayAreaEnter == true) && (attachedObject.active == false) && (busy == false))
{
Invoke("Activate", activateDelay);
busy = true;
}
if ((deactivateOnPlayAreaEnter == true) && (attachedObject.active == true) && (busy == false))
{
Invoke("Deactivate", deactivateDelay);
busy = true;
}
}
function DoOutOfBounds()
{
if ((activateOnPlayAreaExit == true) && (attachedObject.active == false) && (busy == false))
{
Invoke("Activate", activateDelay);
busy = true;
}
if ((deactivateOnPlayAreaExit == true) && (attachedObject.active == true) && (busy == false))
{
Invoke("Deactivate", deactivateDelay);
busy = true;
}
}
function Activate ()
{
print (gameObject + " Checking Activate Conditions!");
if ((activateOnPlayAreaExit == true && isInBounds == false && attachedObject.active == false) || (activateOnPlayAreaEnter == true && isInBounds == true && attachedObject.active == false))
{
print (gameObject + " Activating!");
attachedObject.transform.parent = transform.parent;
transform.parent = attachedObject.transform;
attachedObject.SetActive(true);
}
busy = false;
}
function Deactivate ()
{
print (gameObject + " Checking Deactivate Conditions!");
if ((deactivateOnPlayAreaExit == true && isInBounds == false && attachedObject.active == true) || (deactivateOnPlayAreaEnter == true && isInBounds == true && attachedObject.active == true))
{
print (gameObject + " Deactivating!");
transform.parent = attachedObject.transform.parent;
attachedObject.transform.parent = transform;
attachedObject.SetActive(false);
}
busy = false;
}
I'm having the exact same issue. I have a series of lights which put enemy creatures to sleep. They work fine except when the enemies start inside the lights. In this case ontriggerstay is continuously called no matter the location of the enemy in relation to the light.
I read somewhere else about ontriggerExit not being able to be called without ontriggerEnter having previously been called. As such my solution i guess will have to be to move the enemies into place after the first frame or something similar.
maybe just the first ontriggerstay just call OnTriggerEnter yourself, dont have it do anything just call it so it's logged.
Answer by TutiBueno2 · Aug 04, 2013 at 03:45 AM
IMHO your OnTriggerStayFunction is calling way too many functions. It calls DoInBounds() which repeatedly calls Activate() or Deactivate() and all of them on each frame. So you get an extensive queue of functions to call. Of course when you leave the trigger area the functions will be called anyway unless you use the CancelInvoke() function.