# Stop Light Simulation Game

Hey all,
I’m new to Unity and am currently trying to make a very basic game.

The idea is to have a 4 way intersection. The cars will approach the intersection and based on the state of the light they will either pass through the intersection, or slow down and stop.

The cars do not need to turn, they will only go straight. So right now i just have it so they have preset coordinates to move to and then they stop. But I’m not sure if that is the best way to do it. Also how would i make it so they don’t stop midway through the intersection if the light turns yellow?

So far this is what i have. It is pretty basic and i’m not sure if it will work well… Seems inefficient.

``````var target = 20;
var speed = 1;
var zlocation = 0.00;
var accel = 0;

function Update() {

accel += speed + 0.001;

//Move Car From Initial Point to Destination
transform.position = Vector3(Mathf.MoveTowards
(transform.position.x, target, accel * Time.deltaTime), 0.5, zlocation);
}
``````

Below is what the intersection looks like.
Any help would be greatly appreciated.
THANKS!
Pete

Bumppity bump bump

Place a collider at the “line” where you want them to stop. When they hit that collider, see if the light is red. If so, stop the car. If not, they keep going.

Going along the lines of kieblera5, you could make a variable for yellow/green/red, and put a trigger with a certain radius. If the light is yellow, on trigger enter or stay, slow the car down by a certain amount gradually until it hits the end collar or a certain distance from it. Personally, I’d spawn a collider on red, and then do a simple raycast check against it, while this collider’s mesh render option is turned off as to be an invisible collider, like a plane object. When not red, turn this collider off and then have the car continue driving along. You can even remove the trigger completely, and just check the range of the raycast against this collider (which should also spawn on yellow to work). I’m not sure which is more efficient, though. Eventually you’d probably require waypoints if you want more advanced cars.