- Home /
How can I make simple traffic lights?
I'm making traffic simulation. I want that if the light is red, the collider appears and the car stops and if the light is green, the collider dissapears. Cars have sensors that can notice if something is in front of it. When the sensor notice something, the car breaks. It works if car in head of the other car slows down, the car in the back will break. It works with the collider too. I just want to know how can I make simple traffic lights. I just need green and red light. Any help is appreciated.
sounds like if you were able to code all that other stuff with sensors that this should be a breeze with many possible ways. Are the lights automated with some kind of a timer? You can make the red light collider a child of the red light and just use gameObject.SetActive (true) or (false). But you should probably have a yellow light in there as well?
public Light lightOn;
public Light lightOff1;
public Light lightOff2;
public int timeOn;
Void OnEnable(){
StartCoroutine(DelayBeforLightChange());
private IEnumerator DelayBeforLightChange(){
yield return new WaitForSeconds(timeOn);
lightOn.SetActive (true);
lightOff1.SetActive (false);
lightOff2.SetActive (false);
}
}
//put this on each of the red yellow and green lights and drag into the public variable for each script the light that will be turning on as well as the two that will be off and set the timeOn to how long you want each light to remain on. the red light collider can be a child of the red light.
Or you could go with a timer script and just change the color of a single light with
public Light lt;
publiv int timeOn;
void Start() {
lt = GetComponent<Light>();
StartCoroutine( DelayBeforeLightChange());
}
private IEnumerator DelayBeforeLightChange(){
yield return new WaitForSeconds(timeOn);
lt.color = Color.red;
}
}