Script that Moves Car According to Traffic Light State

Hello all,
So i am in the process of trying to make a traffic light simulation game or sorts.

I guess i’ll start with what i have done… I made it so that the GameObject (The Car) follows the way-points that i put in.

I also managed to make it so that when the light is green the car moves through the intersection, and when the light is red it stops at the light.

Now this is where i’m confused. I figure out how to make it so that depending on how far from the light the car is, it will either stop or continue through the light when the light turns yellow.

I will post the script that i have so far. Please any help would be greatly appreciated.

tl;dr: How do i make the car stop or go depending on how far from the traffic light it is when the light turns yellow.


var nearpoint : Transform;

var farpoint : Transform;

private var hasPassedWaypoint : boolean = false;

private var hasPassedYellowWaypoint : boolean = false;

var speed : float = 5;

private var target : Vector3;

private var moveDirection : Vector3;

private var velocity : Vector3;

function Update () {

MoveThatCar ();

}

function MoveThatCar () {

	//var target : Vector3 = waypoint1.position;

	//var moveDirection : Vector3 = target - transform.position;

	

	//var velocity = rigidbody.velocity;

	



	var distance = Vector3.Distance(nearpoint.position, transform.position);

	

	if(distance <= .1)

	{

		hasPassedWaypoint = true;

	}

if(greenScript2.isEnabled) {

	target = farpoint.position;

	moveDirection = target - transform.position;

	

	velocity = rigidbody.velocity;



	if(moveDirection.magnitude < 1) {

		velocity = Vector3.zero;

	}

	else{

		velocity = moveDirection.normalized * speed;

		}

}

else if (redScript2.isEnabled) {

	if(!hasPassedWaypoint)

	{

	target = nearpoint.position;

	moveDirection = target - transform.position;

	

	

	velocity = rigidbody.velocity;

	}

	

	else if(hasPassedWaypoint)

	{

		target = farpoint.position;

		moveDirection = target - transform.position;

	

	

		velocity = rigidbody.velocity;

	}



	if(moveDirection.magnitude < 1) {

		velocity = Vector3.zero;

	}

	else{

		velocity = moveDirection.normalized * speed;

		}

	

	}

else if (yellowScript2.isEnabled) {

	if(!hasPassedWaypoint)

	{

	target = nearpoint.position;

	moveDirection = target - transform.position;

	

	

	velocity = rigidbody.velocity;

	}

	

	else if(hasPassedYellowWaypoint)

	{

		target = farpoint.position;

		moveDirection = target - transform.position;

	

	

		velocity = rigidbody.velocity;

	}



	if(moveDirection.magnitude < 1) {

		velocity = Vector3.zero;

	}

	else{

		velocity = moveDirection.normalized * speed;

		}

	

	}



rigidbody.velocity = velocity;

}

First, lets set a few variables to use. One for the distance between the car and the light, and for how close you want the car to be from the light to be able to pass through:

var distance : int;
var range : int = 10; //play with this to suite you.
var trafficLight : Transform; // drag your traffic light GO in the inspector

then, lets check how far away we are from the light (this needs to go in update):

distance = Vector3.Distance(transform.position, trafficLight);

now lets check if we are within range!

else if (yellowScript2.isEnabled && distance <= range) 
{
    //your movement script
}

Now all you need to do is, when distance is more than range, put on the brakes!

Hope this helps :).