- Home /
Rotate slowly to target position?
I have this set of code which doesn't work
if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.07f, Screen.width * 0.1f, Screen.height * 0.05f), "Rotate")){
Quaternion neededRotation = Quaternion.LookRotation((GameObject.FindGameObjectWithTag("cube1").gameObject.transform.position - transform.position));
Quaternion.RotateTowards(transform.rotation, neededRotation, Time.deltaTime * 10f);
}
is there any other way to rotate slowly towards the gameobject? Thanks..
Answer by Lovrenc · Jan 12, 2013 at 08:02 PM
You need to run this line multiple times (eg. un Update) to get animation:
Quaternion.RotateTowards(transform.rotation, neededRotation, Time.deltaTime * 10f);
tried it on the update() function but it still doesn't work, i'm trying to rotate the main camera though, thanks for trying to help
nearly 6 years later and this was super helpful. I was using transform.rotation = Quaternion.LookRotation(moveDirection), and this caused immediate choppy turning. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(moveDirection), turnspeed * Time.deltaTime); however gives a nice turn, looks much cleaner and wasn't to word heavy. thankyou
Answer by vincefryer · Jan 21, 2013 at 12:44 AM
Quaternion.RotateTowards() uses the Quaternion base class to find a rotation, but doesn't affect any instance of the class. You need to assign the value it returns to something meaningful (like transform.rotation) in order to see any effect.
Answer by IgorAherne · Oct 22, 2016 at 07:34 PM
transform.rotation = Quaternion.RotateTowards(transform.rotation, neededRotation, Time.deltaTime * 10f);