I have a script which control the light switch (red, green). Now i want the cars, which are following waypoints, to stop at the red light. For the different light colors i use point lights.
I think there is maybe a solution with triggers, but how can i make this exactly?
If I were doing it, I’d have triggers in front of the lights that signal the car to stop. The light sends a message to a script on the trigger, and the trigger interacts with the cars to tell them to stop or go.
With this script it should be possible that the car stops at red light. My problem is now, I dont know what i need to put in the “greenScript2.js”, “yellowScript2.js” and the “redScript.js”, because i´m using one script for the traffic light switch. This one:
var Red : Light;
var Green : Light;
var Yellow : Light;
var GreenTimer : float = 5; // default 5sec.
var redTimer : float = 15; // default 15sec.
var estaVerde : boolean = true;
var estaVermelho : boolean = true;
function Start()
{
Yellow.enabled = false;
while(true)
{
Verde();
yield WaitForSeconds(GreenTimer);
//--------------------------------------------------intermetent yellow light START-----------------------
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.1);
Yellow.enabled = false;
Green.enabled = false;
yield WaitForSeconds(0.2);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.3);
Yellow.enabled = false;
Green.enabled = false;
yield WaitForSeconds(0.4);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.5);
Yellow.enabled = false;
Green.enabled = false;
yield WaitForSeconds(0.6);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.7);
Yellow.enabled = false;
Green.enabled = false;
yield WaitForSeconds(0.8);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.9);
Yellow.enabled = false;
Green.enabled = false;
yield WaitForSeconds(0.10);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(0.1);
//--------------------------------------------------intermetent yellow light END-----------------------
Vermelho();
yield WaitForSeconds (redTimer);
}
}
// green and red lights function to call for others scripts if you need it;)
function Verde()
{
if (estaVerde)
{
Green.enabled = true;
Red.enabled = false;
}
}
function Vermelho()
{
if (estaVermelho)
{
Red.enabled = true;
Yellow.enabled = false;
}
}
That’s a javascript script, which is getting abandoned. wccrawford gave you an answer. The problem is, no one can give you an answer because you don’t know anything. You are using someone else’s scripts for the traffic, and that could be pretty complicated. The code has to fit with that code. Not just a small part of it. Where in the code do cars stop, because you have to affect that? Where do they go again? You would probably have to find which car was getting near the traffic light, so you sent the information to that car, or you could have the car check the nearest traffic light to get the information. Just changing the light color is a minor problem.
If you are going to use someone else’s system like that, you are pretty much stuck with it, unless you can get the author to change it for you. If you want to be able to do that, you have to go through a lot of tutorials and start at the beginning.
What else are you doing that is really so important?
Has a schedule for when the direction changes (can just be every X seconds to start with).
Has an event things can subscribe to for when the available directions change.
TrafficLight:
Has the individual red/yellow/green lights.
Has a TrafficIntersection it belongs to.
Has a direction it applies to.
When the available directions change, sets the lights accordingly.
TrafficIntersectionEntrance:
Has a collider/trigger.
Has a TrafficIntersection it belongs to.
Has a direction it applies to.
When a car hits it, check if the TrafficIntersection is open in this direction.
If not, tells the car to stop and adds it to a list.
When the available directions change, if this direction became open, tell any cars in the list to go.
This doesn’t cover cars past the first stopping (this is a more general situation - they should probably ‘look’ in front of themselves and stop if the way is blocked), and it doesn’t cover all the types of possible intersections (they can be very complex in some cases), but it should be a starting point.
I think it’s too difficult and since it was just to learn something about it with a certain disappointment, I had to abandon the idea mainly because I do not know programming languages.