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 TusharP914 · Jun 18, 2015 at 07:08 PM · c#ontriggerexittraffic

Problem with OnTriggerExit(). If loop not passing to next loop.

I am fairly new to Unity. So I started experimenting with it to create a basic Traffic System. At the moment, there is just one AI Vehicle which stops at red light and continues at green light. I am using the traffic light as the trigger itself. Here's the script:-

  public class LightSystem : MonoBehaviour 
     {    
         public float speed;
         public Light red;
         public Light redDown;
         public Light green;
         public Light greenDown;
         public Light yellow;
         public Light yellowDown;
         public GameObject vehicle;
         public float distance;
     
         void Start()
         {
             StartCoroutine(ChangeLight());
         }
     
         public bool YellowEnabled()
         {
             red.enabled = false;
             redDown.enabled = false;
             green.enabled = false;
             greenDown.enabled = false;
             yellow.enabled = true;
             yellowDown.enabled = true;
             return true;
         }
     
         public bool RedEnabled()
         {
             yellow.enabled = false;
             yellowDown.enabled = false;
             green.enabled = false;
             greenDown.enabled = false;
             red.enabled = true;
             redDown.enabled = true;
             return true;
         }
     
         public bool GreenEnabled()
         {
             yellow.enabled = false;
             yellowDown.enabled = false;
             red.enabled = false;
             redDown.enabled = false;
             green.enabled = true;
             greenDown.enabled = true;
             return true;
         }
         
         IEnumerator ChangeLight()
         {
             while(true)
             {
                 YellowEnabled();
                 yield return new WaitForSeconds(3);
                 RedEnabled();
                 yield return new WaitForSeconds(15);
                 YellowEnabled();
                 yield return new WaitForSeconds(3);
                 GreenEnabled();
                 yield return new WaitForSeconds(15);
     
             }
         }
     
         void OnTriggerStay(Collider vehicleCol)
         {
             speed = 5.0F;
         }
     
         void OnTriggerExit(Collider vehicleCol)
         {
             if(red.enabled == true)
             {
                 speed = 0.0F;
                 Debug.Log ("Pass1");
             }
             else if(green.enabled == true)
             {
                 Debug.Log("Pass2");
                 speed = 10.0F;
             }
         }
     
         void Update()
         {
             vehicle.transform.Translate(Vector3.forward * Time.deltaTime * speed);
         }     
     
     }

Now, The OnTriggerExit() function works well for the first if statement with any condition(i.e green.enabled or yellow.enabled) and prints "Pass1" to the log and the speed is set to 0.0F, but then it doesn't go to the next loop and hence the speed is stuck at 0.0F. By the way, I tried the same with a Raycast and it worked well Here:-

 RaycastHit hitInfo;
         Ray rayInfo = new Ray(trigger.transform.position, Vector3.back);
         Debug.DrawRay(trigger.transform.position, Vector3.back * distance, Color.blue);
         if(Physics.Raycast(rayInfo, out hitInfo, distance))
         {
             if(red.enabled == true)
             {
                 speed = 0.0F;
             }
             else if(green.enabled == true)
             {
                 speed = 10.0F;
             }
         }

But, I want to use trigger instead. Any Solutions will help. Thanks!

[1]: /storage/temp/48434-trficsa.jpg

trficsa.jpg (51.0 kB)
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

1 Reply

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

Answer by Xarbrough · Jun 18, 2015 at 09:41 PM

For the problem: When your object leaves the trigger, OnTriggerExit is called once. If red is enabled, the speed is set to 0, so it stops moving, but is also stuck right outside of the trigger, right? So OnTriggerExit won't be called again.

For the solution: If the raycast worked, why not use it? Seems fine to me.

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 TusharP914 · Jun 19, 2015 at 03:56 AM 0
Share

Thanks! I will use Spherecast ins$$anonymous$$d.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 Answers

Deactivate/Activate FPS Movement 0 Answers

Renderer on object disabled after level reload 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