traffic light help .....

hey guyzz… m creating a project for the childrens who r MR s … i wnat to teach them , hw to deal with city traffic …i had created a city for them … nw am not able to proceed further … m facing problem in makin traffic simulation… i had designed a traffic light … but m nt able to make traffic system work … plz help me …as m new to unity …

Hey guys. I’m creating a project for
the children who are MRs. I want to
teach them how to deal with city
traffic. I have created a city for
them, now I’m not able to proceed
further. I’m facing problems in
making traffic simulation. I had to
design a traffic light, but I’m not
able to make the traffic system work.
Please help me as I’m new to Unity.

What exactly do you need help with? Be specific. How exactly have you built your city? How does one traverse it? On foot? Bike? By car? What part can't you get working? How to make signals change from red to yellow to green and back again? What code have you written already? What are the errors?

–

Did you model a traffic light, and now need somebody to code an entire traffic simulator? Good luck with that.

–

This is something you do with years experience in coding. Humans can't even handle traffic, how would a simulation created by a human be any different? ;-)

–

I'm sorry I get the first part there where you want to change the light at three steps, but I'm having trouble following the actual question. If it's just one traffic light then make use of yields, swap textures for the lights and turn on/off lightsources. If it's a cross section with several traffic lights which is connected to cars moving, hire a coder. ;-)

–

Do not post comments in respones to other people's comments in the answer section.

–

2 Answers

2

Well this is a very old thread, but someone else revived it so lets face the problem. I guess the OP has solved the problem already (or doesn’t need the answer anymore) but since the question is still open I’ve created a “simple” approach.

One way would be to represent each state of a traffic light by a gameobject. This could be the entire model with different textures in each state or lights atached at the right spots with the right color.

I would create one generic script for each traffic light (so no matter if it has 1, 2 or 3 lights) and one script to control the whole system.

I’ve created a sample scene with the whole package included. The two scripts are really simple and most things are managed directly in the scene. The actual sequence is created in the editor.

It simulates the sequence red --> red-yellow --> green --> yellow --> red
(that’s how the traffic lights works in germany :wink: )

If you can’t find the package link on the webplayer sample page above, here’s again the link to the package

This isn’t the best solution (if there is even one that can state it’s the best). It has some redundancies in the assets, but it’s quite simple and you can create any kind of traffic system without changing the code :wink: If this would be a serious project you could add a custom inspector to have a better editor for the sequence, but it works well with the default editor.

Note: The overview is just the whole system duplicated on a seperate layer and the lights turned upwards. The two systems run independently from each other but are in sync, so it’s just an eye candy :stuck_out_tongue:

This does not include the signal for turns at crossroads.

–

Uhmm, this was actually just an example. This is even way too detailed for an example, but yes, sure, you can make a crossroad with 3 lanes each, adding tram-tracks, a seperate bus lane and train-tracks. Maybe a national park in the center with an helipad or even a small airport. Yes, that's how a usual crossroad would look like...

–

@Bunny83: Coming to the actual traffic simulation, its very difficult to implement. Maybe you can get away with few vehicles following traffic rules and also doing obstacle avoidance to avoid collision. The problem rises exponentially with increase in traffic. Implementing correct reversing and U turn logic, it can be made better. But overall it would take years to polish it and make it right... @rakesh91: You's question's headline is about making traffic lights work, but in the description you're talking about traffic simulation. HUGE difference my friend...

–

try with this code

var verde : Light;
var amarillo : Light;
var rojo : Light;


function Start () 
{
	Semaforo();
}

function Update () 
{
	print(Time.time);
}

function Semaforo()
{	
	while(true)
	{
	Apagar();
	yield EncenderVerde();
	yield EncenderAmarillo();
	yield EncenderRojo();
	}
}
public function Apagar()
{
//verde.light.enabled=false;
amarillo.light.enabled=false;
rojo.light.enabled=false;
}

public function EncenderVerde()
{
	this.transform.name="semaforoenverde";
	verde.light.enabled=true;
	yield WaitForSeconds(10);
	verde.light.enabled=false;
}

public function EncenderAmarillo()
{
	this.transform.name="semaforoenamarillo";
	amarillo.light.enabled=true;
	yield WaitForSeconds(2.5);
	amarillo.light.enabled=false;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=true;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=false;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=true;
	yield WaitForSeconds(.4);
	amarillo.light.enabled=false;
}

public function EncenderRojo()
{
	this.transform.name="semaforoenrojo";
	rojo.light.enabled=true;
	yield WaitForSeconds(10);
	rojo.light.enabled=false;
}

for me it worked only you should use two different sequences, using spot light