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
1
Question by digitaldebonaire · May 06, 2014 at 01:55 AM · physicscoroutineupdateontriggerstay

Is there a way to do OnTriggerStay so it isn't ckecked as frequently?

I have a AI object that runs around using pathfinding. I want to check if anything gets in its way and if it does I want to call a function to destroy whatever is in its way. I have been using OnTriggerStay coupled with a contoller.velocity to do that detection but due to how many objects it is colliding with all at once it is really killing performance. Is there a way to implement OnTriggerStay to be checked every 1/100 physics frames or so rather than on every single one? Is this something that could be solved with some kind of coroutine?

I can't use OnTriggerEnter or Exit unfortunately.

 function OnTriggerEnter(other : Collider)
     {
         if(other.gameObject.tag == "Turret")
         {
             var velController : CharacterController = GetComponent(CharacterController);
             var horizontalVelocity : Vector3 = controller.velocity;
             horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z);
         
             //The speed on the x-z plane
             var horizontalSpeed : float = horizontalVelocity.magnitude;
             Debug.Log(horizontalSpeed);                        
             
             if(horizontalSpeed <= blockedSpeed && bombBlasted == false)
             {
              Debug.Log("BOOM");
              UnleashBomb();
              clearPlanes();
              bombBlasted = true;
              //levelMaster.AstarController.Scan();
              levelMaster.explosionEvent = true; 
              GetNewPath();
             }
         }
     }

Because I am checking for a decrease in velocity, I can't get that with an OnTriggerEnter. The above code never runs because by the time the horizontal velocity decreases the OnTriggerEnter has already triggered.

I need the initial collider collision to happen so that the velocity will decrease, then I need the check to be run. Right now the initial collision and the check are run at basically the same time so no drop in velocity is registered without OnTriggerStay

How do I write a collision checking event that happens more often than just once but less frequently than every single physics frame?

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 Jeff-Kesselman · May 06, 2014 at 02:38 AM 0
Share

Can you set a flag that yo hare in collision in OnTriggerEnter and then do your velocity thing in your update ins$$anonymous$$d?

Its not clear to me whats taking all your time but thats what Id do first. The next step would then be to track time in update and only do the check every N ms. Thats on;y if its really your velocity check code thats weighting you down... but from a quick static loo kI don't see anything heinous in it so that would surpass me a little.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by andreyshade · Dec 02, 2020 at 01:12 PM

Try to use this expression to check game object tag

 if (other.CompareTag("Turrent")) {
     //Your code here
 }

It helps you deal with performance/garbage generating issues

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 Jeff-Kesselman · May 06, 2014 at 02:10 AM

Yes.

Don't use OnTriggerStay,

use OnTriggerEnter and OnTriggerExit and keep a list.

There are very,very,very few times when OnTriggerStay is the right answer.

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 koirat · Aug 05, 2014 at 09:48 AM

If you Instantiate a Collider inside Trigger OnTriggerEnter will not be called. If you destroy or disable Collider inside Trigger OnTriggerExit will not be called.

Are you sure you don't want to use OnTriggerStay.

There are very,very,very few times when OnTriggerEnter OnTriggerExit is a reliable solution.

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

23 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 avatar image avatar image avatar image

Related Questions

Weapons that used to work got broken after update - has StopAllCoroutines() stopped working? 0 Answers

Time.deltaTime does not always correctly display the time difference between 2 frames 1 Answer

Is there a way how to use both Animate Physics and Unscaled Time update modes in animator ? 0 Answers

How to Update a GameObject coordinates to folow in realtime and don't lose performens or crushing Unity.[SOLVED] 1 Answer

Game not running properly on slow machines (low fps) 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