- Home /
Trigger actions when avatar enters/exists area
I had got a 2D project in the latest Unity3d. I tried it with overlapping BoxColliders and a single polynomCollider. The avatar has got a CircleCollider
I want to have actions when an avatar (Sprite) enters or exits a certain area (the collider of another Sprite). I do this by incrementing a counter in OnTriggerEnter() and decrementing in OnTriggerExit(). By writing the colliderCounter onto the Console, you will see that moving the avatar in out from the collider the colliderCounter can go negative, or grow over the number of colliders… The trigger functions do not get called the right amount…
Am I missing something? Is there a better way to implement this functionality? Is it a bug in Unity? Is that another way to implement this functionality so that I can work around the bugs? (I'm in a hurry, rather would not wait for a bug fix)
Update: Here's some of my script attached to the avatar:
void OnTriggerEnter2D(Collider2D collider)
{
colliderCounter++;
Debug.Log (colliderCounter);
}
void OnTriggerExit2D(Collider2D collider)
{
colliderCounter--;
Debug.Log (colliderCounter);
}
I see normal things on the console for a while: 0,1,2,3 (this a case with 3 overlapping box colliders) After moving in and out for a while, it I can see -1,-2, or 4,5… These show that some of the trigger actions do not happen.
The same problem exists if I use one polygoncollider.
Answer by Supercrusher9000 · Jan 31, 2014 at 01:58 AM
Are you sure that if you are using OnTriggerEnter() that the colider is checked as trigger? Also make sure that there is a colider on your avatar.
Yes, the collider of the areas are checked as trigger. $$anonymous$$y avatar also has a collider, and the script containing the OnTriggerEnter() and OntriggerExit() functions. The avatar also has a rigid body attached.
I see the OnTriggerExit() and OnTriggerEnter() events working as expected for a while, but after moving in and out for a while it gets out of sync… I'll add a piece of my script to the original post.
Your answer
Follow this Question
Related Questions
walk through an object once 0 Answers
Can someone please tell me why this code isn't working? 1 Answer
OnTriggerEnter function in c# 0 Answers
Make object react to certain triggers only 1 Answer
OntriggerEnter / Stay with the same Gameobject tags 0 Answers