In a Coroutine I'm setting the velocity of a rigid body to follow the touch screen location:
rb.velocity = direction * touchDragPhysicsSpeed;
But I also want its rotation to trend back toward zero degrees. How can I do that?
AI keeps telling me to do something like this:
Quaternion targetRotation = Quaternion.Euler(Vector3.zero);
rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, rotationCorrectionSpeed));
But that MoveRotation line gives the error:
Argument 1: cannot convert from 'float' to 'UnityEngine.Quaternion'
Is this the right approach? If so, how can I fix the error? Or is there a better way?
Sounds like your rb
is actually a Rigidbody2D
in which case indeed rb.rotation
is a float
(the single Z-axis rotation - the only one that matters for 2D)!
you rather want
rb.MoveRotation(Mathf.Lerp(rb.rotation, 0, rotationCorrectionSpeed));
As you want to rotate towards the closest multiple of 360
var target = Mathf.Round(rb.rotation / 360f) * 360;
rb.MoveRotation(Mathf.Lerp(rb.rotation, target, rotationCorrectionSpeed));
This should rotate the shortest way to the closest multiple of 360.
General hint: Instead of blindly trusting AI rather consult the API ;)