I created a 4WD , i want to calculate the distance travelled by the car using only wheel colliders , based on the motor torque applied to the wheels.
I’m not sure but :
float _distanceTraveled;
Vector3 _lastPosition;
void Start()
{
_lastPosition = transform.position;
}
void Update()
{
_distanceTraveled += Vector3.Distance(transform.position, _lastPosition);
_lastPosition = transform.position;
}
You may convert the angular velocity of the wheel into linear velocity using the radius and accumulate it:
float distance = 0.0f;
void FixedUpdate ()
{
float angularVelocity = myWheelCollider.rpm * (2.0f * Mathf.PI) / 60.0f;
distance += angularVelocity * myWheelCollider.radius * Time.deltaTime;
}
Note that this solution doesn’t have the wheel slip into account. If the wheel is slipping in place, then the distance will still be increased as per the distance traveled by the surface of the wheel.
Solution above