Traffic light turns green when the car reaches?
I'm new to Unity and using Unity to design a auto-driving police car for my project, I've had traffic lights that has 3 lights, now what I want is when the car reach the traffic light, if the light is red or yellow it should turn to green. I have wrote a script with OnTriggerEnter to change the light but the problem is the light turns green but other color is on at the same time. What I'm thinking is to pause the light function when the car reach the trigger, change the light with OnTriggerStay, and resume the light function with OnTriggerExit. Could anybody give hint how to pause a function? and is this a good way to do? Here is my script:
public class triggerenter : MonoBehaviour { public Light red; public Light yellow; public Light green;
void OnTriggerEnter()
{
if (green.enabled = true)
{
Debug.Log("Light is green");
}
else if (yellow. enabled = true)
{
yellow.enabled = false;
green.enabled = true;
Debug.Log("light change from yellow to green");
}
else
{
red.enabled = false;
green.enabled = true;
Debug.Log("light change from red to green");
}
}
}
and the traffic light script: public class trafficlight : MonoBehaviour {
public Light green;
public Light yellow;
public Light red;
public float greentimer;
public float redtimer;
public float yellowtimer = 2;
IEnumerator Start()
{
if (yellow != null)
{
yellow.enabled = false;
}
else { Debug.Log("yellow light not defined"); }
while (true)
{
green.enabled = true;
red.enabled = false;
yield return new WaitForSeconds(greentimer);
yellow.enabled = true;
green.enabled = false;
yield return new WaitForSeconds(yellowtimer);
yellow.enabled = false;
red.enabled = true;
yield return new WaitForSeconds(redtimer);
}
}
}
Answer by Toon_Werawat · Apr 09, 2016 at 03:23 PM
Here. I got whole new script for you
using UnityEngine;
public class TrafficLight : MonoBehaviour
{
public Light greenLight;
public Light yellowLight;
public Light redLight;
//Instead of make 3 light count. Just make only one.
public float LightCount;
public enum state
{
red = 0, yellow = 1, green = 2, yellow2 = 3
}//Here is light state. Also. Reason why we have 2 yellow? Because enum can act. Like int. And so if we want to toggle to next state. We just use + 1 on it. And it will be weird if it just go from red to yellow to green. Then immediately red.
public state LightState = state.yellow;
//Light state. Default yellow.
public bool emergency;
//In case of emergency. Toggle.
void Update()
{
if (!emergency) // Are we have emergency? If not. Do this.
{
LightCount -= Time.deltaTime; //Count lightCount down to 0
redLight.enabled = LightState == state.red;
yellowLight.enabled = LightState == state.yellow || LightState == state.yellow2;
greenLight.enabled = LightState == state.green;
//This is a shorter version of
/*if (LightState == state.red)
{
redLight.enable = true;
yellowLight.enable = false;
greenLight.enable = false;
}
if (LightState == state.yellow.....blah blah blah*/
//This is what we do. When light count to 0
if (LightCount <= 0)
{
//Add light state up. By 1
LightState += 1;
//It act like int. So it will go up more then 3 and we have to cap it down to 0. But it not really int. We have to cast it.
if ((int)LightState > 3)
{
LightState = 0;
}
//After we add LightState up. We start counting again. If we at yellow light? Give it 2 Seconds.
if (LightState == state.yellow || LightState == state.yellow2)
{
LightCount = 2;
}
//If we at red or green light? Give random number between 20 - 50 seconds
else if (LightState == state.red || LightState == state.green)
{
LightCount = Random.Range(20, 50);
}
}
}
else //Do this if emergency.
{
greenLight.enabled = true;
yellowLight.enabled = false;
redLight.enabled = false;
}
}
/When enter trigger.
void OnTriggerEnter()
{
emergency = true;
}
//When leave trigger
void OnTriggerExit()
{
emergency = false;
}
}
Thank you very much! This is much better than my method. $$anonymous$$ay I ask one more question, in your code when the emergency is on the light does change to green but once the car exits the trigger it immediately changes to another color, I tried to add an Invoke function to delay the OntriggerExit for few seconds but it didn't work. Could you please let me know how to do it?
You can use.
Coruetine
void OnTriggerExit()
{
StartCoruetine(RestoreEmergency());
}
or Invoke
void OnTriggerExit()
{
Invoke("RestoreEmergency",2f);
}
And at "RestoreEmergency"
void RestoreEmergency()
{
emergency = false;
}
Ah, I didn't create a new function that's why. It works fine now, thanks a lot!