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 dee7800 · Jul 26, 2013 at 03:49 AM · triggertriggersmultipletagsexit

how to cancel OnTriggerExit when already hitting a 2nd trigger

my issue an object moving when it hits the "floor" it triggers a flag variable, "not_on_floor", to get set to 0 and then when it stops touching the "floor" i change the flag back to 1. The flag variable is then used in the update function to cancel out and adjust movements while it is is touching the "floor".

the problem is when there are two objects present with the tag "floor" that are within close proximity. The object stops touching the first "floor" while it has already begun touching the second "floor". Upon exiting the first "floor" the flag gets momentarily changed changed to 1 before the OntriggerStay resulting from the second "floor" can push it back to 0.

this results in the movement basically skipping a beat.

what i would like is for the not_on_floor flag to stay 0 until it is not touching any of the triggers tagged with "floor"

the object is a kinematic ridgedbody with a box collider.

below is the code controlling the flag.

 function OnTriggerStay(other: Collider){
       
       if (other.tag == "floor"){
         not_on_floor=0;
       }
 
 }
 
 function OnTriggerExit(other: Collider){
   if (other.tag == "floor"){
     not_on_floor=1;
   }
 
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jul 26, 2013 at 05:13 AM

You could use OnTriggerEnter/Exit and count the events:

 private var qTrigger = 0; // count OnTriggerEnter
 
 function OnTriggerEnter(other: Collider){
     if (other.tag == "floor"){
         qTrigger++;
         not_on_floor = 0;
     }
 }
  
 function OnTriggerExit(other: Collider){
     if (other.tag == "floor"){
         qTrigger--;
         if (qTrigger == 0){ // this was the last trigger
             not_on_floor = 1;
         }
     }
 }

}

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 dee7800 · Jul 26, 2013 at 06:33 AM 0
Share

Thanks aldonaletto worked great. i was using enter the first time and tried to use the stay to hold but that just made it not work in another way. The counting works perfect and a simple fix. Thanks a lot!! i just started learning unity on my own so it was a big help actually and gave me more insight on how to structure. I had a feeling that i was wasting resource with the stay.

avatar image
0

Answer by usernameHed · Mar 25, 2019 at 07:42 PM

The Answer of aldonaletto works great, but it's not enought if you have multiple object. Here is my suggestion:

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GravityAttractorTrigger : MonoBehaviour
 {
     [Serializable]
     public struct TriggerCountExit
     {
         public GameObject objectTriggered;
         public int count;
 
         public TriggerCountExit(GameObject obj, int _count)
         {
             objectTriggered = obj;
             count = _count;
         }
     }
 
     [SerializeField, Tooltip("enum to interact")]
     private List<string> tagList = new List<string>()
     {
         “Player”,
         “Enemy”,
         “Object”
     };
 
     [SerializeField, Tooltip("enum to interact")]
     private List<TriggerCountExit> countCancelExit = new List<TriggerCountExit>();
 
     private int ContainObject(GameObject obj)
     {
         for (int i = 0; i < countCancelExit.Count; i++)
         {
             if (obj.GetInstanceID() == countCancelExit[i].objectTriggered.GetInstanceID())
                 return (i);
         }
         return (-1);
     }
     
     private bool ManagerEnterCancel(GameObject other)
     {
         int contain = ContainObject(other);
         if (contain != -1)
         {
             TriggerCountExit newTrigger = countCancelExit[contain];
             newTrigger.count++;
             countCancelExit[contain] = newTrigger;
             return (false);
         }
         else
         {
             countCancelExit.Add(new TriggerCountExit(other, 0));
             return (true);
         }
     }
     private bool ManageExitCancel(GameObject other)
     {
         int contain = ContainObject(other);
         if (contain != -1)
         {
             TriggerCountExit newTrigger = countCancelExit[contain];
             newTrigger.count--;
             if (newTrigger.count < 0)
                 newTrigger.count = 0;
             countCancelExit[contain] = newTrigger;
 
             return (countCancelExit[contain].count == 0);
         }
         else
         {
             //error, can't be
             return (false);
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (tagList.Contain(other.tag))
         {
 If (ManagerEnterCancel(other.gameObject))
             {
                 //here action enter for the first time
 }
 else
 {
     //here action for the second, third time…
 }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (tagList.Contain(other.tag))
         {
                     if (ManageExitCancel(other.gameObject))
                     {
                         //here you can do your exit action
                     }
                 } 
             }
         }
  }
 }
 
 

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

17 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

Related Questions

checking to see if multiple (different) tags are destroyed 2 Answers

Multiple triggers on one object 8 Answers

Child Object's collider setting off triggers 1 Answer

multiple enemies does nt work with this script but a a single enemy works..Please help 0 Answers

Why does the money counter resets when I pull different objects trough the trigger? 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