Writting a road safety VR application I had to create bot vehicles.
Please note that no NDA has been breached in this article.
There we go, I had it. Now how can I implement that in Unity?
The road was composed of sections, each section having a mesh collider matching the flat part of the road but also the external vertical plane matching the limits of the road section.
To keep the 3D car in the current lane, we have to compute where we want to place the car in the next frame, so that the car will stay at the same distance from the road side.
To do this, I computed a point ahead of the current position, P_Ahead, of the car (using simple local to world transformation). From this point a ray was cast in a direction matching the (right) side of the car (World X axis of the car). If the ray hits the vertical planes located on the side of the road at P_Collision, I move along the normal of the ray-plane collision point to compute P_DriveTo. The norm of the P_Collision P_DriveTo vector is the distance (DSide) we want between the car position in the next frame and the side of the road.
Great, now that we have P_DriveTo, the point that we want the bot car to be in the next frame, we have to steer the driving wheel so that the car reaches this position.
Steering is actually changing the front wheels angle and what we are looking for is this steering angle. At this point I used:
And voila, we can get the steering angle to change the front wheels orientation to point the car towards the P_DriveTo destination point. We make this computation at each frame of course, and slowly changing DSide, we can achieve a lane change. If we change DSide an abrupt way, we can simulate a car avoiding an obstacle on the road as it just consists in brutally steering. Not changing DSide makes the car stays in the current lane.