Concept of trafficlightsystem with colliders

Hi together,

I already created the switching between the diffrent states of the trafficlights, so
my main problem is to inform the cars the states of the trafficlights. I wanted, that the
information flow only should be solved over the colliders!
More precise: The 2 functions OnTriggerEnter(col: Collider) and OnTriggerExit(col: Collider).

If the streetlane got the state GREEN, no collider should be on the streetlane. (The car can just drive through, performance aspekt :))
If the streetlane got the state RED, a collider should appear and the OnTriggerEnter(col : Collider of the
car would check its a specific collision(col == trafficlight).

The Problem:
My idea was, if a car drive into the collider (RED state), it stops.
Now the car-collider are inside the trafficlight-collider!
And when the GREEN state gets activated: the collider gets deactivated, moved or resized (*laugh)
and the function OnTriggerExit(col: Collider) of the car should be activated. Well that was my plan :).
As I was experimenting and my knowledge increased, I understood, that:

  1. You need the rigidbody(But that was not the main problem because the car always got one)!
  2. It always had to be a force applied to the object in oder to go in the OnTriggerEnter() or OnTriggerExit() functions!

So resizing, deactivating or moving(translate) just doenst work in my case!

I really like this concept. Because the flow of the information is determined through these 2 functions.

I can see 2 solutions about this:

1.Is to apply a ridgibody to the trafficlight collider and apply a force up in the air,
in order to trigger the function OnTriggerExit(). I think this solution is more like the crowbar method.

  1. Use the function OnTriggerStay(col: Collider). If I deactivate the collider the function stop to Trigger.

Maybe I missed something or someone else got better idea how I could solve this with colliders?

And a last question:
Could someone imagine to use this Trafficlightsystem for its own game? (OpenSource style Models inclusive)

Thank You for your time and reading! :slight_smile:

many greetings geneziz

I would go for a completely different solution. Have your trigger keep a list of all the cars inside it (adding them in OnTriggerEnter, removing them in OnTriggerExit). Then whenever the traffic light changes, send a message to all the cars inside the trigger. This way, the cars cannot only respond to red and green, but also have an appropriate reaction to orange lights.

At First:
Thank you for answering and sharing your ideas in this topic tomvds :slight_smile: !

I like your idea, tomvds.
If would follow your idea, then I would create a script for the cars that contains the follwing methods:

  • GotRedSignal()
  • GotGreenSignal()
  • GotYellowSignal()
  • GotRedYellowSignal() (lose the break ;))

The script could then controll the “carscript”.

My first idea was to create something that doesnt have to store the cars. It just give instant information to its world through the colliders.
I saw this method as something very easy, because it got a very lose dependendcy. It doesnt have to know which cars are inside the bounds.

If the trafficlightsystem have to know which cars to send a message to, the dependendcy increase and make the system more vulnerable.

Do you know what I mean?

many greetings geneziz

If you don’t want the traffic light system to know about cars, you could do one of two things:

  • Use the Unity SendMessage functionality to send the traffic light messages to anything inside the collider. This way the collider doesn’t have to know about cars, it knows only about GameObjects that are inside itself. That way, if you would have additional objects that aren’t cars (e.g. cyclists) could also use the traffic light functionality by simply implementing the functions called by SendMessage.

  • Switch from a call back system to a polling approach. Have the collider keep the state of its associated traffic light and have any cars inside check this variable to see the state of the light. That way, traffic lights do not need to know about cars, but cars need to know about traffic lights (which makes sense).