- Home /
RotateTowards with deltaTime Causes Spinning!
I am working in 2D and trying to get a unit to turn slowly toward a position.
To do so, I've tried using RotateTowards in several forms.
Quaternion targetRotation =
Quaternion.LookRotation(Vector3.forward, pather.currPath.vectorPath[1]-transform.position);
// This gets the target rotation where "up" is pointed toward the target position.
// pather.currPath.vectorPath[1] is the Vec3 point I'm trying to point toward.
transform.rotation = Quaternion.RotateTowards(transform.rotation,targetRotation,15f*Time.deltaTime);
// Expected behavior: turn 15 deg/sec until facing the right way, then stop.
// Actual behavior: turn continuously, spinning slowly without end.
The problem only occurs when I add in Time.deltaTime. If the last parameter is just a number like 15f, it turns rapidly to the correct rotation. When I set transform.rotation = targetRotation, the same happens, which means I should be able to use deltaTime to rotate at a fixed rate, but the object just keeps spinning.
I have also tried using vectors:
transform.up = Vector3.RotateTowards(transform.up, pather.currPath.vectorPath[1]-transform.position, 15f*Time.deltaTime*Mathf.Deg2Rad, 100f);
// Same idea as above using Vector3.RotateTowards.
When I try this code using fixed values instead of 15f*Time.deltaTime*Mathf.Deg2Rad, it snaps to the right rotation for values above ~.01f and spins endlessly for values below.
Multiplying by Time.deltaTime in RotateTowards is normal in the documentation, so I don't think there should be any floating point error or anything. What could be causing this?
Your answer
Follow this Question
Related Questions
Rotate Towards doesnt work as expected 2 Answers
Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer
Digital Clock Needed For New UI Text 1 Answer
Whats the source code of Quaternion.FromToRotation? 1 Answer
Perpendicular Vector3 0 Answers