[SOLVED] Rotate Toward Target Using AddTorque

Problem:

  1. Rigidbody needs to rotate using AddTorque toward other object (Camera)
  2. There is not need to stop at exact position some overshoot is ok
    Solution:
  3. Using quaternions this is not an issue so those solutions are known and working
    What is done:
  4. Direction in what object should rotate is:
Quaternion.LookRotation(Camera.transform.position - object.position)
  1. in FixedUpdate AddTorque is:
object.AddTorque(normalizedRotateToward*rotationSpeed, ForceMode.VelocityChange);
  1. rotationSpeed is float that determins Speed
  2. normalizeRotateToward is Vector3 that represents “force” that we apply to rotate object
  3. Later in FixedUpdate detection of overshoot detection is:
if (Quaternion.Angle(object.transform.rotation, initialRotation) > targetAngleDifference)
{
    object.AddTorque(-object.angularVelocity, ForceMode.VelocityChange);
    normalizedRotateToward = Vector3.zero;
}
  1. targetAngleDifference is angle for detection of overshoot from initialRotation toward targetRotation

If someone knows how to get torque that will rotate object toward targetRotation (LookRotation) and catch overshoot please post a solution because this is driving me nuts.
If precision was needed I would go for Quaternions Lerp route and that is already done.
Thanks in advance.

    Quaternion rot=Quaternion.LookRotation(target.position-rb.position,Vector3.up);
    rot=Quaternion.Slerp(Quaternion.identity, rot*Quaternion.Inverse(rb.rotation), 1.0f);
    rb.AddTorque(new Vector3(rot.x,rot.y,rot.z)*10f);
1 Like

That worked like a charm!
Here is full solution: