Simple traffic AI?

Hello, I’m using a raycast for my car object to detect other cars in front of it, and if there is any - the car that casts a ray should be deleted (simply so I could see that it’s working)
But for some reason there is no detection at all, neither of the cars gets deleted and I don’t know why, do you have any ideas?


The objects you’re raycasting against is the car itself. When you do a repeat for each car object, the list of cars in the sub events of that repeat is just that one car object. So you’re testing the raycast against obejcts car, it’s just using that one car, and it will never hit it.

A solution would be to have a separate invisible object on each car (link it to the car) and repeat for all those objects instead.

1 Like

Thanks for the reply, but this is not exactly what I need, or I am probably doing something wrong. At the beginning of the scene I create invisible collision objects and attach them to each one of the cars.
Each car sends a ray in front of it, if it hits any collision object - the car raycast is sent from should turn to “slow mode”, if raycast hits nothing, the car switches to “fast mode”. The problem is that the raycast affects the car it hits, not the car it is sent from. Also, they are not switching back to “fast mode”, once the car is found by other cars raycast it is forever in “slow mode” for some reason
(Currently, Im not switching any speed modes for the cars and just changing their color, 1 - slow, 0 - fast.)

So, lets say it is a row of cars, only the rightmost car should be green(fast), because it has nothing in front of it, I get the opposite, as you can see.

My response is correct for the screen shot in your earlier post. But you’ve changed your events. You’re now raycasting against Collision, and not Cars as in your previous screen shot.

No, it changes the Collision the raycast hits. Instead of setting the Collision animation to 1 (as snipped below), change the tint of Car to black. You’ll see the car the raycast came from change to a black rectangle.

1 Like

Thanks, now I see. But still, I cant understand why cars don’t switch back to “fast mode” if their raycast hits nothing, I’ve done similar stuff many times and it always worked, I’m confused as hell :upside_down_face:

Where do you set the car’s mode?

1 Like

Each car has a boolean “SpeedMode”, depending on its state, the variable “Speed” changes, which is used to set the speed of the cars movement.

Currently, I’m just switching the tint of car objects as you suggested, just to see if it works


I wonder if that effect is because of :

I’d set the car tint to 255;255;255 in an unconditional event and as the first subevent of the repeat block.

1 Like

Yes, it is working that way. But I’ve done simple AI for hostile pedestrians with pathfinding behavior, they had a boolean “Alert”, if it was true they chased the player, and wandered around if it was false, each one of them sent a ray against the group (walls and player) which determined the state of “Alert”. If num of Player > 0 - set boolean to true, and if num of Player </= 0 set boolean to false. And it worked just fine. Well, this way of doing this is ok too, thank you.

Yes, I agree. Looking at your events, it should change tint back to normal if there are no collisions.

What happens if, instead of using “The number of collisions is less than or equal to 0”, you used “The number of collisions is less than 1”?

Because it may be a numeric error, where 0 is getting stored as some really, really small number, rather than an absolute zero. This is just a stab in the dark, so to speak.

1 Like

I’ve tried this, didn’t work. For some reason I often get weird stuff like this when I use other programs like blender or photoshop along with Gdevelop, restarting Gdevelop sometimes fixes such things, but not this time. I have very little understanding of what is going on under the hood of Gdevelop, maybe raycast actually should hit just any false object, like a wall, to consider the number “player” is 0, i dont know

Hi 7ToGo

I know that this is an old post but would it be possible to help me out. I wanted to know how you coded the flow of traffic in your game. You see I am doing something similar and would love it if you could help me out. I left a link to my what I am doing at the moment below.

1 Like

Hi, I never figured out how to make a traffic system similar to the first GTA chapters, where cars could not only drive through the streets along a random path and stop at traffic lights, but also, for example, follow the player or react to the environment. I’m sure it’s possible to do something similar in Gdevelop, but I suspect it’s very tricky. If you need cars to just drive along pre-prepared paths and stop at traffic lights - try using the “Curved Movement” extension.

1 Like

Thank you for replying Sir.

I was thinking of using the prepared paths. then I thought what if I add a force to the cars and then use your ray cast code to detect cars from one another, then use a collision stop and start timer at the traffic lights and then a random script after the timer is complete(traffic light is green) to let the car decide if it wants to go left or right depending on on the road. So the I would have to make 4 different collision the traffice. A traffic light collision to stop and start again. A corning collisions for non traffic light roads and a 4 way stop and start collision. Then I would have to make a random link car vision collision to check if the car wants to stop for the player or not. Let me know what you think. I haven’t tested the logic yet but I would love to know your thoughts.

Again thanks again for replying.

I also tried to find similar solutions, but they are completely unsuitable for at least relatively large maps, too much manual work for the sake of a very simple and inflexible system.