[SOLVED] Make item follow a path

Hi all

Let’s say I have a 3D landscape which includes a road. I want an item (e.g. a car) to follow that road.
The user should not be able to “steer” the car’s direction (it should always stick to the road), but only control its speed (e.g. via up and down buttons on the keyboard).

In PlayCanvas, what is the best way to achieve this?
Is there anything like a “guiding line” / “path” object which I can tie to the road, and then let the car object follow that?

All input is appreciated. Thanks a lot!

There are many ways to do it.
You could just create points in space, and have list of them in order of follow. Then interpolate position of item between current and next point, till it gets close to it, then switch to next point, and so on.

You could interpolate different ways as well, it depends on your gameplay, if item has to follow string linear path, or have inertia like a car on fast speed. Depending on your requirements you might use simple lerp between positions of point positions, or perhaps have a vector of movement, and point it towards next point, and change its direction based on speed (faster speed, harder to turn), or even make dependency on speed, like if it is far from point, it will turn little bit, but getting closer will more likely to make quick turns.

It very depends on your requirements.
Just start experimenting, step by step moving towards getting your ideas expressed in code.

1 Like

There are examples of pathfinding code in roguelike and in tower defence i guess it’s the name of the project

Another option is to export an animated node from your 3D tool and attach the car to this node. Then you are simply playing back the animation and you can control the animation playback speed to move faster or slower.

Guys! I’m overwhelmed, thanks a lot for the quick and helpful answers!