- Home /
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
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.
Your answer
Follow this Question
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