If sentence while object is running

Hi,
Some of my students want to change a traffic light from green to red if a car is near the traffic light. I tried it using an if and a distance blocks, but the if block doesn’t work.
any advice?
Thank you very much.

1 Like

Hi @Joseju,

There can be at least two approaches:

  1. Calculate distance between car and traffic light. There should be a variable to check if it is already in red state before turning red, otherwise the code will constantly play turning red animation from beginning and it will not look great.
  2. Create a sphere (or semi-sphere) around traffic light and check if the car collides with it. Sphere can be made transparent, so the user will not see it.

I implemented both options in the following space. Press q-a and w-s to drive cars.
You can remix the space to see details and copy the code

1 Like

Thanks a lot Nikolay. I Think great both solutions. I have tried your scene and that is exactly what I want, but “remix” option isn’t available in order to see the code. Can you allow it?
Thanks again

Hi @Joseju,

The sharing settings of the space were updated! You should be able to remix it now :slight_smile:

Thanks!! :+1: :clap::clap:

This was helpful for me too.

Hey Nikolay, Thanks. I was looking for that and it worked for me

1 Like