# Vehicle AI

Hi people,

I’m making a game that is a driver simulator inside a city. So, there will be some stop points that all the computer controlled cars will have to stop.

I could do my car travelling using a path and sterring using WheelCollider but I could not realize how to stop at a certain point in the street, like in a traffic light.

I’ve tested using a break value [0,1] that is calculated from the area available for stopping but my vehicle always stop before, even my break value being 0.4, for example.

Could someone give me some help?

Thanks![/quote]

If you know the car’s forward speed (given by rigidbody.velocity.magnitude), and the current rate of deceleration/braking, you can calculate the distance from the current point that the car will stop. Say, for example, the car is travelling at 5 metres per second and is decelerating by 1 m/s every second, it will take 5 seconds to come to a standstill. Generally, the time taken to stop is given by

``````brakingTime = speed / deceleration
``````

The distance away from the current point that the car will stop is given by the formula:-

``````distance = (speed * brakingTime) / 2.0
``````

You can use this distance to vary the braking force as the car approaches the target.

Start off by using a guess for the value of brake force to use. If you measure the car’s speed at the end of the frame update and again at the start of the next update, you can find the rate of deceleration (it’s just the difference between the two speed values). You can plug this into the formula given above to find the distance at which the car will stop.

Basically, if the stopping point is farther away than the target, then you need to brake harder. If it’s before the target, then you need to ease off the brakes. If you divide the actual stopping distance by the target distance, you will get a fractional overshoot/undershoot value. If it is less than one, the car will stop too soon, and if greater than one, it will stop too late. If you multiply this overshoot/undershoot value by an “average” brake force (that you obtain with a bit of trial and error), the car should come to a stop reasonably close to the target point.

1 Like