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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by zhed · Nov 27, 2013 at 09:06 PM · rigidbodycollidertriggerontriggerenterontriggerexit

OnTriggerEnter/Exit called unexpectedly

Hi, I have a capsule(the NPC body) with a cone mesh attached(their sight). This sight cone has attached a trigger collider and a kinematic rigidbody. This NPC is patrolling, so he walks and looks around. The problem is that I'm using another capsule(the player), which has both a collider(not trigger) and a kinematic rigidbody, to test the NPC sight(basically, I'm moving this character so to enter the field of view of the NPC), but OnTriggerEnter/Exit on the sight cone on the NPC is called unexpectedly: when the player capsule enters the trigger mesh of the NPC, suddenly onTriggerEnter and onTriggerExit get called twice each(and yes, in both onTriggerEnter and onTriggerExit i check that the object entering/leaving is the player collider). The same thing happens when the player collider leaves the trigger mesh of the NPC, onTriggerEnter and onTriggerExit get called twice each.

To make things a bit clearer, this appears in the Console:

Player Entering Player Entering Player Leaving Player Leaving

Does anybody have any idea about what's going on here?

Comment
Add comment · Show 11
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 oneslyfox · Nov 27, 2013 at 09:30 PM 0
Share

Is it possible that the NPC sight mesh is intersecting with the NPC's capsule? You can do a quick check in the console like this:

 function OnTriggerEnter (col : Collider) {
     
     print(col.gameObject.name);
     
 }
avatar image zhed · Nov 27, 2013 at 09:40 PM 0
Share

unfortunately, i already checked, but no, that's not happening :\

avatar image oneslyfox · Nov 27, 2013 at 09:46 PM 1
Share

This might seem silly, but you don't have duplicates of the script on the object, do you? Other than that, I'm stumped.

avatar image OP_toss · Nov 27, 2013 at 09:58 PM 0
Share

I would filter out the collisions by making sure they are indeed the player. Use Tags or Layers or w/e you wish, but make sure the collider entering the trigger is what you think it is.

Also add some simple print debugging like so:

 void OnTriggerEnter( Collider col )
 {
     Debug.Log( col.gameObject.name + " is entering trigger " + this.gameObject.name );
 }

And the same for exit.

That should tell you exactly what is going on, and should probably make the problem become obvious. Hope so anyways! Post with results if not!

avatar image OP_toss · Nov 27, 2013 at 10:01 PM 0
Share

Another thought... Is your cone collider a mesh? If so, that could be causing the immediate enter/exit behavior, if it has bad normals, is not a closed object, convex is wrong, etc...

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by senyedenes · Oct 14, 2016 at 10:23 PM

Hy,

This is because Physics can be called multiple times before each update method. I had a very similar issue and my solution was something like that:

 function OnCollisionEnter (collision : Collision) {
      if(!collisionAlreadyTracked) {
           // collision related code...
      }
     collisionAlreadyTracked = true;
 }

This simple statement basically prevents these events to be tracked multiple times per frame. I guess there is a more sophisticated way to handle this, so it is just a quick workaround, but its working. :) (until you (and me as well) get a better solution)

https://docs.unity3d.com/Manual/ExecutionOrder.html

Comment
Add comment · 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
0

Answer by belvita · Nov 29, 2013 at 01:53 PM

why dont u attach an empty game object with a box collider to your gameObject and use the box collider to check if OnTriggerEnter is called box colliders work far better than capsule colliders

Comment
Add comment · 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

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

OnTriggerEnter mesh collider problem 1 Answer

OnTriggerEnter not being called, have trigger, RB, and colliders set? 1 Answer

OnTriggerEnter works, OnTriggerExit doesn't 3 Answers

Compound Triggers with no intermediary exit calls 1 Answer

Rigidbody + Multiple Trigger Colliders - OnTriggerEnter - Get which Collider triggered the event issue 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