- Home /
How to run what's inside OnTriggerEnter temporarily while the two objects are in contact?
void OnTriggerStay2D (Collider2D other) {
if (other.gameObject.tag == "Hand") {
onHand = true;
}
}
So basically, I want onHand to be true only when the two objects are in contact. As soon as they come out of contact I want onHand to be false again. Any easy way to do this?
There are callbacks like OnTriggerEnter, onTriggerStay ,OnTriggerExit .
And in your case make onHand =true when you got the call in OnTriggerEnter() and then set it to false , when you got the call in OnTriggerExit().
Answer by ransomink · Apr 04, 2014 at 05:45 AM
Use OnTriggerExit2D when an object leaves a trigger collider. You should not just set onHand = false; You will need to check if the GameObject that left is tagged "Hand" because any other GameObject that has collided with the trigger and left will set onHand to false, but the GameObject tagged "Hand" could still be colliding.
void OnTriggerExit2D ( Collider2D other )
{
if ( other.gameObject.tag == "Hand" )
{
onHand = false;
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter only working once. 0 Answers
Trigger area around player. 1 Answer
Becoming parent after OnTriggerStay/Enter? 1 Answer
Won't Enter GetButton Statement 1 Answer
Show message when gameobject hits other gameobject.. 1 Answer