How to calculate car braking force (using wheelcollider)?
Hello,
I am making a simple car AI, which idea is to automatically spawn vehicles and generates a path for each one on a random logic. I have created so far the driving mechanics, the turning mechanics and the breaking ones. But I'm having difficulties knowing when to apply the brake. Imagine the situation : You have a car stopped at a red light and there's one coming from behind on full speed, how can I calculate the breakforce which I have to apply in order for it to stop relatively close and realistic behind the other car?
Im using simple Raycast forward in order to detect other cars. private void Sensors() { RaycastHit hit; Vector3 sensorStartPos = transform.position;
if (Physics.Raycast(sensorStartPos, transform.forward, out hit, sensorLength)) {
Debug.DrawLine(sensorStartPos, hit.point);
if (hit.transform.tag == "Car") {
CarEngine otherCar = hit.transform.GetComponent<CarEngine>();
Debug.Log("Hit");
}
}
}
In order to apply the brakes, all you need to do is set isBraking = true
and set a braking force if needed.