How can i make traffic cars they driving self and randomly on a path

Greetings, this is my first time trying to make a car game with playcanvas. The car controlled by the player is more or less ok. But I haven’t been able to get the other cars in traffic to go randomly on the road. I would appreciate any suggestions from those who have experience in this matter. The method I have tried right now was to put 3 boxes in front of and on the sides of the car, check whether the car is on the road with them, then move the car forward, turn it to the right and left, but it was not very successful. Could there be a simpler method, such as the cars in traffic randomly following a curve drawn on the road, or a suggestion for the (raycast) method I have tried. Thank you

Hi @potemkin and welcome,

You can create a path curve based on a list of entities in editor, and use that to animate the cars. Here is an example project:

1 Like

Hi Leonidas, i seen this tutorial but couldn’t make it.In this tutorial camera move on path but i don’t know how can i use another entity.Because camera has target and this confuse me.It is possible,can you make it for a box entity and share pleas?

Even though the script is named camera-path you can use it to animate any other entity, not only a camera.

Just attach it to the entity you would like to animate, set the attributes and voila:

1 Like

Ok thanks leonidas, i will try it again.

Ok I tried it and it works as you said. But I think creating a car traffic with this method is not flexibel at all. Because it’s an animation as a result, and it will not be easy to generate randomness in a traffic composed of a lot of cars. For this, I have continued with the raycast method before, and for now the car continues to stay on the road. I think it is more logical and more difficult to continue with this method. I sent the link of the project.
I expect suggestions from members who have more experience in this field. Thank you.

The cameraPath moves the entity along a path over time but that can be changed to be more ‘random’ such as varying in speed, slowing down around corners and speeding up on straights.

It will require extra work on top of the existing code to detect this but given the road model you have shown, it should work.