Calculate angle between motion and facing direction

Hey,
I am currently trying to make a Semi Realistic WheelCollider from scratch… I have to calculate the distance the wheel traveled. But this is not so easy because if I eg. slide down a hill (sideways) the wheels should not move… So i have to calculate the angle between the motion and the facing direction… My approach was :

``````    var angle=Vector3.Angle(Car.rigidbody.GetPointVelocity(hit.point),Wheel.transform.forward);

distance=(Car.rigidbody.GetPointVelocity(hit.point).magnitude*Time.deltaTime)*Mathf.Abs(Mathf.Cos(angle));
``````

But this is not working correctly, the wheels are spinning in a strange way and are also rotating too slow… I have been trying to figure this out for 2 hours now and I dont get “forward”, I hope you can help me…

`Wheel.forward` is the blue +z arrow coming out of the wheel. Unity thinks x is sideways, y is up and z is forward.

The obvious problem with wheel.forward is that the wheel spins, so its forward could be down, up or backwards. Car.forward would be more consistent (or do the wheels also rotate to “steer”? You might want “flat” angle (if you go straight, and down a hill, the angle should be 0, but the down-ness will be part of the angle.) So:

``````Vector3 moveDir = Car.rb.<your code>;
moveDir.y = 0; // ignore slope
Vector3 faceDir = Car.forward; // car.f instead of wheel.f
faceDir.y=0; // ignore slope
angle = <your code>
``````

I know this is old but, it’s new to me. In my space game I have physics based movement (AddRelativeForce), but I also wanted to cap the speed. I did this by creating a ratio of the max speed verses the current speed and applying it to the thrust. The issue with this was that if i turned, it didn’t properly calculate the amount of thrust to allow in the new direction based on the current speed. I fixed this using a ratio built from the angle between the rigidbody direction and the transform facing direction and adding it to the speed ratio. That came out more complicated than I wanted, so here is the code:
class variables

``````		private float thrust = 0.0f;
private float actThrust = 0.0f;
private float thrustRatio = 0.0f;
private float dispSpeed;
private float rotAngle;
``````

The answer to the question asked is here in this section. rotAngle = Vector3.Angle (this.transform.forward, _cacheRigidbody.velocity). But I posted all of this figuring that everyone was probably trying to solve the same, physics based space movement problem.
in my Update()

``````if (thrust < -10.0f)
thrust = -10.0f;
if (thrust > 100.0f)
thrust = 100.0f;
if (!thrustChange && Mathf.Abs (thrust) < 1.0f)
thrust = 0.0f;
if (dispSpeed <= thrust) {
thrustRatio = 1;
} else {
thrustRatio = thrust / dispSpeed;

}
rotAngle = Vector3.Angle (this.transform.forward, _cacheRigidbody.velocity);
dispSpeed = _cacheRigidbody.velocity.magnitude * 1000;

actThrust = (thrust * (thrustRatio + (rotAngle / 180.0f)) * 0.54f);
if (thrust * 5 < dispSpeed)
actThrust = -0.10f;
``````

in my FixedUpdate()

``````_cacheRigidbody.AddRelativeForce (Vector3.forward * 100f * Time.deltaTime * actThrust);
``````

It works for me… ok. Rhere is this weird thing where the speed picks up a little unexpectedly when I turn. But I think it’s good enough for now. I hope this helps someone. BTW the rigidbody mass was set to 10000, drag = .05 and angular drag = 1. It’s for a slow moving capitol ship.