Problem:
Rigidbody needs to rotate using AddTorque toward other object (Camera)
There is not need to stop at exact position some overshoot is ok
Solution:
Using quaternions this is not an issue so those solutions are known and working
What is done:
Direction in what object should rotate is:
Quaternion.LookRotation(Camera.transform.position - object.position)
in FixedUpdate AddTorque is:
object.AddTorque(normalizedRotateToward*rotationSpeed, ForceMode.VelocityChange);
rotationSpeed is float that determins Speed
normalizeRotateToward is Vector3 that represents “force” that we apply to rotate object
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;
}
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:
Here is full solution including @zulo3d great post from here:
Get InputAction.CallbackContext rotatePerformedInputValue from method that hooks on Mouse movement:
initialRotation = object.transform.rotation;
targetAngleDifference = rotationIncrement; // 45
rotationDone = false;
normalizedRotateToward = Vector3.zero;
normalizedDeltaMove = Vector2.Normalize(rotatePerformedInputValue.ReadValue<Vector2>());
Get InputAction.CallbackContext resetRotationPerformedInputValue from method that hoo…