Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SloppyDepot · Jan 15, 2014 at 06:17 AM · 2dcollidertrigger

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:

alt text

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.

Trigger Bug.zip

trigger bug.zip (144.5 kB)
trigger_bug.jpg (197.3 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jan 15, 2014 at 09:38 AM 1
Share

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?

avatar image SloppyDepot · Jan 15, 2014 at 10:04 AM 0
Share

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.

avatar image Benproductions1 · Jan 15, 2014 at 10:22 AM 1
Share

Use a collection that doesn't have duplicate items, or manage that yourself.

avatar image SloppyDepot · Jan 15, 2014 at 10:33 AM 0
Share

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.

avatar image AgentKnopf · Aug 30, 2015 at 10:48 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SloppyDepot · Jan 16, 2014 at 03:22 AM 0
Share

I'm a little embarrassed I didn't think of this myself, but it does solve my problem. Thanks a lot!

avatar image Benproductions1 · Jan 16, 2014 at 05:45 AM 0
Share

No problem, glad I could help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision with renderer.enabled? 0 Answers

I can't get 2DCollider to work. 1 Answer

Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer

why isn't my OnTriggerEnter2D() function working? 14 Answers

Why doesn't OnTriggerEnter2D get called? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges