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 gitoffame · Apr 11, 2013 at 01:58 AM · collisiontriggerfixedupdateontriggerstay

OnTriggerStay Weirdness

This seems to keep calling OnTriggerStay, even long after the other collider has moved away from it. Any insight into why this is happening, and how to make it stop?

So a little context:

My game has a moving brush, tagged "PlayArea", which contains all the action, basically. The playarea uses a mesh collider, because the collider needs to be wedge-shaped for my purposes. This is supposed to determine whether the collider is in contact with the playarea's collider, and swaps objects for a placeholder object, depending whether or not they're within the playarea. It works just fine as far as activating objects, but not so much when it comes to deactivating them.

I'm trying to get around using an OnTriggerExit, because I want it to work independently of whether or not it started out within the playArea brush.

Here's the code:

 #pragma strict
 var activateDelay:float = 0;
 var deactivateDelay:float = 0;
 var activateOnStart:boolean;
 var deactivateOnStart:boolean;
 var activateOnPlayAreaEnter:boolean;
 var deactivateOnPlayAreaEnter:boolean;
 var deactivateOnPlayAreaExit:boolean;
 var activateOnPlayAreaExit:boolean;
 private var attachedObject:GameObject;
 private var isInBounds : boolean = true;
 private var busy : boolean = false;
 
 function Start ()
     {
     
     attachedObject = transform.parent.gameObject;
     
     if (activateOnStart && attachedObject.active == false)
         {
         //print (gameObject + " starting inactive, activating!");
         Activate();
         }
         
     if (deactivateOnStart && attachedObject.active == true)
         {
         //print (gameObject + " starting active, deactivating!");
         Deactivate();
         }
     }
 
 function Update ()
     {
     }
 
 function LateUpdate()
     {
     }
     
 function FixedUpdate()
     {
     //print (gameObject + " FixedUpdate check isInbounds = " + isInBounds + "!");
     if (isInBounds == false)
         {
         //print (gameObject + " DoOutOfBounds!");
         DoOutOfBounds();
         }
     isInBounds = false;
     }
     
 function OnTriggerStay (col:Collider)
     {
     if (col.gameObject.tag == "PlayArea")
         {
         isInBounds = true;
         DoInBounds();
         //print (gameObject + " TriggerStay Check True!");
         }
     }
     
 function OnCollisionStay (col:Collision)
     {
     if (col.gameObject.tag == "PlayArea")
         {
         isInBounds = true;
         DoInBounds();
         //print (gameObject + " CollisionStay Check True!");
         }
     }
 
 function DoInBounds()
     {
     if ((activateOnPlayAreaEnter == true) && (attachedObject.active == false) && (busy == false))
         {
         Invoke("Activate", activateDelay);
         busy = true;
         }
     if ((deactivateOnPlayAreaEnter == true) && (attachedObject.active == true) && (busy == false))
         {
         Invoke("Deactivate", deactivateDelay);
         busy = true;
         }
     }
     
 function DoOutOfBounds()
     {
     if ((activateOnPlayAreaExit == true) && (attachedObject.active == false) && (busy == false))
         {
         Invoke("Activate", activateDelay);
         busy = true;
         }
     if ((deactivateOnPlayAreaExit == true) && (attachedObject.active == true) && (busy == false))
         {
         Invoke("Deactivate", deactivateDelay);
         busy = true;
         }
     }
 
 function Activate ()
     {
     print (gameObject + " Checking Activate Conditions!");
     if ((activateOnPlayAreaExit == true && isInBounds == false && attachedObject.active == false) || (activateOnPlayAreaEnter == true && isInBounds == true && attachedObject.active == false))
         {
         print (gameObject + " Activating!");
         attachedObject.transform.parent = transform.parent;
         transform.parent = attachedObject.transform;
         attachedObject.SetActive(true);
         }
     busy = false;
     }
 
 function Deactivate ()
     {
     print (gameObject + " Checking Deactivate Conditions!");
     if ((deactivateOnPlayAreaExit == true && isInBounds == false && attachedObject.active == true) || (deactivateOnPlayAreaEnter == true && isInBounds == true && attachedObject.active == true))
         {
         print (gameObject + " Deactivating!");
         transform.parent = attachedObject.transform.parent;
         attachedObject.transform.parent = transform;
         attachedObject.SetActive(false);
         }
     busy = false;
     }
Comment
Add comment · Show 3
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 Batlad · Aug 04, 2013 at 12:55 AM 0
Share

I'm having the exact same issue. I have a series of lights which put enemy creatures to sleep. They work fine except when the enemies start inside the lights. In this case ontriggerstay is continuously called no matter the location of the enemy in relation to the light.

avatar image Batlad · Aug 04, 2013 at 01:02 AM 0
Share

I read somewhere else about ontriggerExit not being able to be called without ontriggerEnter having previously been called. As such my solution i guess will have to be to move the enemies into place after the first frame or something similar.

avatar image sparkzbarca · Aug 04, 2013 at 03:25 AM 0
Share

maybe just the first ontriggerstay just call OnTriggerEnter yourself, dont have it do anything just call it so it's logged.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TutiBueno2 · Aug 04, 2013 at 03:45 AM

IMHO your OnTriggerStayFunction is calling way too many functions. It calls DoInBounds() which repeatedly calls Activate() or Deactivate() and all of them on each frame. So you get an extensive queue of functions to call. Of course when you leave the trigger area the functions will be called anyway unless you use the CancelInvoke() function.

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

13 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

Related Questions

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

OnTrigger problems with a sphere enabling/disabling targets on collision 1 Answer

OnTrigger event when colliders are already touching eachother 1 Answer

Detect when a trigger has entered a collider 1 Answer

OnTriggerStay still called after moving a parent gameobject 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