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 Xertz · Oct 05, 2014 at 01:35 AM · colliderontriggerentermonobehaviourontriggerexitontriggerstay

MonoBehaviour.OnTriggerX vs Collider.OnTriggerX

I haven't found this question anywhere.

If you check Collider's and MonoBehaviour's description (comparing OnTriggerStay function) you would see that the Collide one "is called almost all the frames" and the other "is called once per frame".

I'm using an empty game obj with a trigger box collider witch has all OnTrigger functions (no need for a rigidbody), checking against moving kinematic rigidbody game objs with non-trigger box collider.

The logic I'm trying to accomplish is when is not triggering with any other obj, then spawn a new moving obj.

 void OnTriggerEnter( Collider other )
 {
     Debug.Log("Enter");
     isTriggering = true;
 }
 
 void OnTriggerStay( Collider other )
 {
     Debug.Log("Stay");
     isTriggering = true;
 }
 
 void OnTriggerExit( Collider other )
 {
     Spawn();
     Debug.Log("Exit");
     isTriggering = true;
 }

The new spawned obj will be to the right of the empty obj, a few units inside the collider, so that way the next frame OnTriggerEnter would be called (each obj move to the left each frame). Then OnTriggerStay is called, and lastly when the obj gets outside the trigger collider, OnTriggerExit is called. In other words, one trigger function should be called every frame. Should be.. but sometimes neither of them get called. That's why I'm using the isTriggering flag.

 void LateUpdate()
 {
     if( !isTriggering )
     {
         Spawn();
         Debug.Log("Trigger failed");
     }
 
     isTriggering = false;
 }

As you can see in the image below I'm using Collider's OnTrigger functions, and what I want to ask is how can I manage to force these functiones to get called every frame? (to use MonoBehaviour triggers).

Trigger failure

I've tried with Courutines and Invokes every frame but OnTrigger functions need a Collider obj as parameter to check with whom collided with, and that info is provided by the Unity Event system.

Note: I'm using LateUpdate to check if no trigger collision was detected and to reset the flag.

untitled.png (16.8 kB)
Comment
Add comment · Show 1
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 Xertz · Oct 05, 2014 at 03:49 PM 0
Share

So why there are two variants of OnTrigger and OnCollider functions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Oct 05, 2014 at 02:30 AM

The docs are wrong about it being called every frame; they should say that it's called every physics frame. It's not possible to make it be called every frame.

Comment
Add comment · Show 3 · 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 Xertz · Oct 05, 2014 at 03:47 PM 0
Share

So you say $$anonymous$$onoBehaviour's OnTrigger functions are called as OnCollision functions? every FixedUpdate?

avatar image Eric5h5 · Oct 05, 2014 at 05:08 PM 0
Share

Only OnTriggerStay is called every physics frame; OnTriggerEnter/Exit are only called once. Same with collision.

avatar image Xertz · Oct 05, 2014 at 07:34 PM 0
Share

Yes, you are right. I know that Trigger events are separated between three actions:

  1. Enter: when is the first time a trigger collision is cough (if the last frame there wasn't a collision)

  2. Stay: if the last frame was cough a trigger collision.

  3. Exit: if this frame was not cough a collision.

The problem that I'm having (even with OnTriggerStay) is that is not being called every frame. I checked that with the isTriggering flag.

I think the reason is that OnTriggers are being called sometimes every physics frame, compared with OnCollisions, which are called each frame.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerExit is not calling 2 Answers

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

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

Can someone clarifies OnTriggerEnter, OnTriggerExit and OnTriggerStay for me? (I have image included already) 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