- Home /
Best way to have vehicles follow a path?
I am making a game that has AI controlled vehicles drive around some roads, completely autonomously, but I'm having a very difficult time getting the movement to be smooth and look natural.
They key things the vehicles need to do are:
Follow a road that has some turns and curves as it goes around hills
Slow down, stop, wait for other vehicles, and then accelerate through a stop sign
Slow down if a vehicle is stopped in the road
Initially, I found a SplineController library that did #1 very well, #2 okay and did not do #3. I tried modifying the library, but found it to be very convoluted and wrote my own using WheelCollider and RigidBody component to accelerate and steer, using a custom set of waypoints to define the road.
This had problems because the physics of the WheelCollider and RigidBody were too accurate, and these roads are stylized and simplistic and have some turns that are too sharp for them to handle.
I replaced the RigidBody and WheelCollider with a CharacterController that ignores physics, but the steering is very jerky or awkward and does not look natural. I tried to calculate out the correct turning and realized I'm basically mimicking a bezier curve.
I looked through the Asset Store a little bit to find things related to vehicles or rails or splines, but nothing stood out as the right fit, and I don't want to just drop $10 on hope.
Is there a way to easily do what I am trying to do? I'm willing to spend a little money on assets if that solves this problem.
Answer by kingcoyote · Apr 02, 2015 at 06:35 PM
I ended up using the SuperSplines library in the Asset Store and it worked great. I was able to define splines and have the vehicles follow the splines. I had to add in some extra logic around the intersections for the yielding, but that was easy enough to do.
Answer by static_cast · Nov 29, 2014 at 02:14 AM
This is a great question for the unity forums, but I'll put my two bits here...
Make some waypoints in an array that tells it what to do.
Example (which is slightly similar to what you did):
[location, speed, curve]
[(0,1),50mph, true]
[(1,2),50mph, true]
[(1,3),30mph, false]
There ya go! :)
Also take a look at this: http://answers.unity3d.com/questions/386662/how-to-create-waypoints-for-a-car-to-follow.html
Thanks for the answer, but like you said, that's essentially what I did. Simply saying "curve" is like telling a wordworker to just "cut the wood". Calculating that curve is fairly complicated and eluding me. The rads per sec will vary based on a few factors - distance to the waypoint, angle difference between the start angle and final angle (and how is that deter$$anonymous$$ed? line from start to end? tangent from end to end+1?), even speed has a factor (less rads per sec in a slow speed intersection than a high speed curve).
Your answer