• 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
1
Question by mattimus · Jul 12, 2012 at 04:49 AM · ontriggerenterontriggerexit

OnTriggerEnter / OnTriggerExit behaves erratically

I'm getting very unreliable results from using OnTriggerEnter() and OnTriggerExit().

  • When I move a collider into a trigger volume, it will sometimes trigger [Enter, Exit, Enter] over the course of about 4 frames, rather than just the single [Enter]. This happens even when the collider is moving in a linear fashion perpendicular to the trigger volume edge, so it's not being caused by the collider swerving in and out of the volume.

  • When a collider sits idle inside a trigger volume, I will occasionally get a random [Exit, Enter] pair over about 2 to 10 frames that causes flickering on the object being controlled by the volume. I put a debugging line with the frame number on both enter and exit. While a collider with rigidbody sits inside the volume, I get something like this:

    Volume Enter: 622 Volume Exit: 673 Volume Enter: 675 Volume Exit: 685 Volume Enter: 687 Volume Exit: 760 Volume Enter: 762

  • If I move in and out of trigger volumes fast enough, I can sometimes get the volume to stick in the 'on' position without having a player inside the volume. This comes from Enter being called and the player quickly leaving the volume without the Exit call.

The objective is to have something similar to a button that turns on/off depending on whether or not a player is standing on it. I want to use OnTriggerEnter and OnTriggerExit instead of OnTriggerStay because I don't want to have to poll every frame to determine if a player is on the button. Is there a more reliable way to accomplish this?

Comment
Add comment · Show 2
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 whydoidoit · Jul 12, 2012 at 06:35 AM 0
Share

Hmm that's odd - I've used the technique you want to apply before and never had a problem. Is there perhaps an animation or something that is causing the effect? Does this happen when the object is firmly inside the trigger volume (I'm wondering if it could be a floating point inaccuracy)

avatar image thaiscorpion · Jul 12, 2012 at 09:53 AM 1
Share

$$anonymous$$e too I have also used OnTriggerEnter() and OnTriggerExit() with no problems can you post the code inside these functions maybe there is something in there?

2 Replies

· Add your reply
  • Sort: 
avatar image
-2

Answer by programmrzinc · Jul 12, 2012 at 11:36 AM

When you just say ONTriggerEnter() you are telling the program to do the event when ANY collider passes the threshold(.

 var Coll : Collider
 OnTriggerEnter(Collider Coll){
 
 }

this could raise the event ONLY when the Collider comes in.

Comment
Add comment · Show 1 · 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 TheDemiurge · Apr 17, 2013 at 08:58 AM 2
Share

In case anyone comes across this answer... not only is this incorrect (specifying a parameter does not limit the trigger to that collider, merely allows you to get info about it), but you're also mixing JS and C# in there.

avatar image
2

Answer by mattimus · Jul 14, 2012 at 01:57 AM

I've located the problem.

I have collision covering the whole character's body, and then a small collider inside the character's pelvis that is used for trigger detection. This ensures the trigger fires when the player is all the way on the button, not just one toe on it. The main player collision is set to not interact with the trigger layer through the physics properties, but it still seems to be interfering with the OnTriggerEnter and OnTriggerExit. I have disabled the main character collision and the trigger detection collider in his pelvis now fires the triggers properly.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Can someone clarifies OnTriggerEnter, OnTriggerExit and OnTriggerStay for me? (I have image included already) 0 Answers

Problem using trigger to set gameobjects active 1 Answer

Deactivate/Activate FPS Movement 0 Answers

OnTriggerEnter works once, OnTriggerExit not at all? 1 Answer

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 Answers


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