- Home /
Is this normal behavior for edge collider triggers (2D)?
I'm trying to create a tower defense game, and I want to prevent the player from placing towers on the path. I tried using colliders (set as triggers) to accomplish this, but I'm running into a problem where the OnTriggerEnter2D and OnTriggerExit2D calls don't match up. OnTriggerEnter2D is called more often than OnTriggerExit2D, which makes it impossible to keep track of the number of invalid objects the tower is touching.
I've created a new project to isolate the behavior... This is what I have:
There's two game objects. Both have sprite renderer components. Both have rigidbody 2D components. The circle has a circle collider 2D component. The path has an edge collider 2D component. Both colliders are triggers. Neither rigidbody components are kinematic.
The circle game object has the following script:
using UnityEngine;
using System.Collections;
public class CircleScript : MonoBehaviour {
private int _collisionCount = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
newPosition.z = 0;
transform.position = newPosition;
}
void OnTriggerEnter2D(Collider2D collider)
{
_collisionCount++;
Debug.Log ("on enter. count: " + _collisionCount);
}
void OnTriggerExit2D(Collider2D collider)
{
_collisionCount--;
Debug.Log ("on exit. count: " + _collisionCount);
}
}
The script moves the circle to the mouse position, and handles calls to OnTriggerEnter2D and OnTriggerExit2D.
The behavior I was hoping for was for the trigger Enter/Exit calls to match up so that _collisionCount would be zero when the circle isn't touching the edge collider. But for some reason, they don't match up, and _collisionCount ends up being greater than zero because OnTriggerEnter2D is called more frequently.
If I edit the edge collider so that it only has 2 vertices, then I get the behavior I expect. It seems like each line segment making up the edge collider is calling OnTriggerEnter2D correctly, but randomly missing some calls to OnTriggerExit2D.
EDIT: Now that I think about it, it seems that OnTriggerExit2D is being called the correct number of times (once for the entire edge collider), and OnTriggerEnter2D is being called too many times (once for each line segment of the edge collider).
Is this a bug, or am I just doing something wrong? I'll attach the project to this post.
Thanks.
If OnTriggerEnter
is called more times than OnTriggerExit
, then you're not entirely exiting a trigger before entering another. Can I suggest the use of a collection to keep track which triggers you are in?
If I use a collection and add the trigger to it during OnTriggerEnter2D, and then remove it during OnTriggerExit2D, I still have the problem where I have triggers left in the collection when the circle isn't touching anything.
It's because OnTriggerEnter2D is called multiple times for the same collider (once for each line segment, it seems), and OnTriggerExit2D is only called once for the whole collider.
Use a collection that doesn't have duplicate items, or manage that yourself.
Ah, good call. If you want to post that as an answer, I can go ahead and mark it as the answer since it solves my problem.
Your question really helped me because I was having the same problem. Since in my case it's just about items crossing an edge collider I decided to use OnTriggerExit2D ins$$anonymous$$d of OnTriggerEnter2D as the latter seemed to rogue fire occasionally and I wasn't able to figure out why/how.
Answer by Benproductions1 · Jan 16, 2014 at 01:08 AM
Hello,
The reason OnTriggerEnter
is called more than once, is because it represents each collision found. An object might collide with another one multiple times, while still being inside of it. OnTriggerExit
is only called when the object is entirely out of the trigger object. If you want to keep track of which trigger objects you are inside of, you need to keep a collection that doesn't have any duplicate items, such as a dictionary where the key is the trigger object and the value could be a boolean, or you can just manage that yourself by checking if the object is already in the collection before adding it into it.
Hope this helps,
Benproductions1
I'm a little embarrassed I didn't think of this myself, but it does solve my problem. Thanks a lot!
Your answer
![](https://koobas.hobune.stream/wayback/20220613130502im_/https://answers.unity.com/themes/thub/images/avi.jpg)