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;
}