- Home /
Traffic Light System
I am currently creating a Car Parking and Driving Simulation game but I'm having a problem making it. Currently what I successfully implemented
Park the Car
Game Over when the Car Crash into something
Traffic Light System
The problem is that if my car go forward when the traffic light is green and then suddenly it turns out to be red on the other traffic light it will trigger the next collider and I don't want that because it is a mistake right? . Please see the image below.
The StopAndGoHandler GameObject has the script called StopAndGoHandler.cs this handles if the traffic light is green you can go,yellow prepare to stop but still you can go,red to fully stop. Now if the car go forward even if the traffic light is red then that comes the
Violation GameObject which has the script TrafficViolation.cs
Here's the StopAndGoHandler.cs
public GameObject tlc;
public Text[] Obey_Traffic_Rules;
private void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("Player")){
TrafficLightSignal();
}
}
private void OnTriggerExit(Collider other){
if(other.gameObject.CompareTag("Player")){
TrafficRulesInit();
}
}
public void TrafficLightSignal(){
var TLC = tlc.GetComponent<TrafficLightHandler>();
Debug.Log (TLC.Trafficlight.ToString());
if (TLC.Trafficlight == TRAFFIC_LIGHT.GREEN) {
TrafficRules(Obey_Traffic_Rules[0], true);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
} else if (TLC.Trafficlight == TRAFFIC_LIGHT.YELLOW) {
TrafficRules (Obey_Traffic_Rules [1], true);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
TrafficRules(Obey_Traffic_Rules[0], false);
} else if (TLC.Trafficlight == TRAFFIC_LIGHT.RED) {
TrafficRules(Obey_Traffic_Rules[2], true);
TrafficRules(Obey_Traffic_Rules[0], false);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[3], false);
} else {
// nothing
}
}
public void TrafficRules(Text t, bool val){
t.gameObject.SetActive (val);
}
public void TrafficRulesInit()
{
TrafficRules(Obey_Traffic_Rules[0], false);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
}
Here's my TrafficViolation.cs that inherets StopAndGoHandler.cs
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("Player")){
_TrafficViolation();
}
}
private void _TrafficViolation(){
var TLC = tlc.GetComponent<TrafficLightHandler>();
if (TLC.Trafficlight == TRAFFIC_LIGHT.RED) {
TrafficRules (Obey_Traffic_Rules [3], true);
TrafficRules (Obey_Traffic_Rules [0], false);
TrafficRules (Obey_Traffic_Rules [1], false);
TrafficRules (Obey_Traffic_Rules [2], false);
} else {
TrafficRules (Obey_Traffic_Rules [0], true);
TrafficRules (Obey_Traffic_Rules [1], false);
TrafficRules (Obey_Traffic_Rules [2], false);
TrafficRules (Obey_Traffic_Rules [3], false);
}
}
By the way tlc is my TrafficLightSystem which just has an enum
public enum TRAFFIC_LIGHT
{
GREEN,
YELLOW,
RED
};
my TrafficLightHandler.cs just changes the material of a sphere from red,green and yellow.
Someone please help me out if I'm doing this game correctly if not could you please guide me. Thank you.
Answer by jman12EX · Mar 21, 2019 at 07:25 AM
you could make the colliders only on the side of the road that the cars drive's on. (eg if a, US game make it so the trigger is only on the right-hand side) this is so the car going through will not trigger it off as a violation.
Answer by Robertelis · Mar 21, 2019 at 07:44 AM
If the car passed the collider while the light was green let it pass throught other colliders without getting a violation. Maybe try something like:
bool legalCarMove = false //if the car passed a collider while it was green, let it pass other colliders for x secoonds
if (legalCarMove != false)
{
//code to let the car pass other colliders without getting a violation
}
else
{
//get violation
}
Also try placing the collider on the stop line itself and if the car crosses it after the traffic light is red you get a violation. To avoid the problem of the player staying in the middle of the road while the light is red, you could implement another collider that checks if the player is staying in there for x amount of seconds.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help with a collider script 1 Answer