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 PotatoInsertion · Jul 22, 2014 at 09:31 AM · triggerontriggerexitontriggerstay

Getting triggers to stop triggering on some objects once certain criteria is met

Hi. Apologies for the weird question title, found it really hard to sum up in a sentence.

I've a game at the moment where you need to place books onto a shelf, upright. This is done with triggers; when you drag a book into the trigger area and drop it, if it stays upright it's marked as correctly placed and the trigger material turns green. That is all done in OnTriggerStay. And then in OnTriggerExit, the material colour goes back to normal once the book is moved out of the area, and it's marked as nothing being placed there.

The above is working. But when a book is correctly placed, and then I try and drag another book into the trigger area, and then out again, it's marked as nothing being correctly placed there, despite the original book still being placed there — I have to click the correctly placed book for it to marked as placed. I've tried a check by saving the gameobject that's correctly placed, and then making OnTriggerExit only fire when that book exits, but I can't get it to work. (this isn't in the code below, but the gameobject being saved is 'book', which still is)

Has anyone any ideas?

// EDIT I've since recoded some parts, so putting them below. But the issue seems to be something to do with how close the object are to each other; see my comment below.

 void OnTriggerEnter(Collider col){
 
         if (col.gameObject.tag == "Draggable"){
 
             //print ("Enter col: " + col.gameObject);
             
             objectAttemptingEntry = col.gameObject;
         }
 
         if (bookPlaced && objectAttemptingEntry != objectPlaced) {
 
             ignore = true;
         }
         else {
 
             ignore = false;
         }
     }
 
     void OnTriggerStay (Collider col) {
 
             if (col.gameObject.tag == "Draggable"){
 
 
                 if (!ignore){
 
                     //print ("staying col: " + col.gameObject);
 
                     objectAttemptingStay = col.gameObject;
 
                     if (objectPlaced == null || objectPlaced == objectAttemptingStay){
 
 
                         if (objectPlaced == null){
                             print ("Object Placed is Null. " + objectAttemptingStay + " is attempting Stay");
                         }
 
                         if (objectPlaced == objectAttemptingStay){
                             print ("Object Placed is objectAttemptingStay. " + objectAttemptingStay + " is attempting Stay");
                         }
 
                         GameObject temp = objectAttemptingStay;
 
                         DraggableItem item;
                         item = temp.GetComponent<DraggableItem>();
 
                             // check the orientation of the book type to see if it's close enough to be correct, depending on the orientation specfied above
                         if (     (CloseEnough(temp.transform.rotation.x, orientationF_up_A, 0.5f)) 
                                 ||  (CloseEnough(temp.transform.rotation.x, orientationF_up_B, 0.5f)) 
                                ||  (CloseEnough(temp.transform.rotation.x, orientationF_down, 0.5f)) ){
 
                             // correct book, correct orientation
                             gameObject.renderer.material.color = Color.green;
                             bookPlaced = true;
                             item.inPlace = true;
 
                             objectPlaced = temp;
                         } 
                         else {
                             // correct book, wrong orientation
                             gameObject.renderer.material.color = Color.red;
                             bookPlaced = false;
 
                             item.inPlace = false;
                         }
                     }
                     else {
 
                         objectAttemptingEntry = null;
                         //objectAttemptingStay = null;
                     }
                 }
         }
     }
 
     void OnTriggerExit (Collider col) {
 
         if (col.gameObject.tag == "Draggable") {
 
             //print ("Exit col: " + col.gameObject);
 
             objectAttemptingExit = col.gameObject;
 
             if (objectAttemptingExit != objectPlaced){
 
                 print ("objectAttemptingExit is not the placed object. Returning");
                 objectAttemptingEntry = null;
                 objectAttemptingExit = null;
                 return;
             }
             else {
 
                 if (objectPlaced){
 
 
                     print ("objectAttemptingExit is objectPlaced. Exiting.");
 
                     DraggableItem item;
                     item = col.gameObject.GetComponent<DraggableItem>();
                         
                     gameObject.renderer.material.color = Color.white;
                     bookPlaced = false;
                     item.inPlace = false;
                     objectPlaced = null;
 
                     objectAttemptingExit = null;
                     objectAttemptingStay = null;
                 }
 
             }
         }
     }

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 Klarax · Jul 22, 2014 at 09:57 AM 0
Share

cant you just place a bool statement around it all so if its done it sets it to false, not allowing them to enter the code anymore?

avatar image PotatoInsertion · Jul 23, 2014 at 01:39 PM 0
Share

That doesn't seem to work. Everytime I put in checks like that, it seems to ignore them, or the OnTriggerExit resets them when I don't want them to.

I'm trying to get it so if the col.gameobject that is exiting isn't the original gameobject that's there,(the one that's saving to 'book') then it simply returns out of OnTriggerExit, but it's ignoring that check for some reason.

Likewise, I'm trying to get it so the OnTriggerStay function code only fires when the book object is null. But that also seems to get ignored.

avatar image PotatoInsertion · Jul 23, 2014 at 02:55 PM 0
Share

I've narrowed it down that it's something to do with distance between the objects, or something like that.

I've added in loads of checks to see what's exactly going on, and still couldn't figure it out. But then I made the trigger area much bigger, and have now noticed that it's something to do with how close the objects are. If I move a new object into a trigger area that has a book placed correctly in it, everything works at first; I can drag in a bit, drag it out a bit, and everything works as it should. But if I move it to within a certain distance of the placed book object, then the triggers seem to think that this new object is now the placed object, so everything breaks down.

It doesn't seem to be just distance though. Sometimes if I drag in from the left, I can get the objects pretty close together before it goes wrong, other times I bring it from the top or elsewhere and it breaks almost immediately. I've checked the colliders of the two objects as well; it can break whether they overlap with each other or not.

I'm finding this problem to be really frustrating as it doesn't seem to make any sense.

I've edited my question above with the new code I've done since. Though now I'm not so sure if it's even a coding problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PotatoInsertion · Jul 24, 2014 at 10:29 AM

I eventually scrapped all this code and redid it with a spherecast. Now instead of a trigger checking for books, a spherecast comes out of the trigger area and does the job.

As I couldn't get this to work with Triggers in the end, this doesn't really answer my question, so I won't mark this answer as correct. If someone could figure out an answer the actual question, I'll happily mark it.

From what I could figure out, it's something to do with proximity of the two objects. When I moved an object close (how close or not doesn't seem to be a constant figure) to a correctly placed object (whether their colliders overlapped or not didn't seem to matter), then the correctly placed object's OnTriggerExit function would call; despite there not being any movement to that object. This happened whether the object being moved (ie, not the one correctly placed inside the trigger) entered the trigger area or not.

Makes no sense to me anyway. Maybe it's a Unity bug, or maybe I was just missing something.

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

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

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

Trigger animation going crazy 3 Answers

How can I make an enemy hurt the player? 3 Answers

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