- Home /
Use Lerp to rotate object
 void Update () {
         transform.Translate (Vector3.right * speed * Time.deltaTime);
 
         if (Input.GetKeyDown (KeyCode.W)) 
         {
             transform.localEulerAngles = new Vector3(0,0,90);
         }
         if (Input.GetKeyDown (KeyCode.D)) 
         {
             transform.localEulerAngles = new Vector3(0,0,0);
         }
         if (Input.GetKeyDown (KeyCode.S)) 
         {
             transform.localEulerAngles = new Vector3(0,0,-90);
         }
         if (Input.GetKeyDown (KeyCode.A)) 
         {
             transform.localEulerAngles = new Vector3(0,0,180);
         }
     }
with this simple arrangement i am able to rotate the object but how can i rotate it smoothly.
I can not find lerp function which accepts current rotation and future rotation and some float.
please help.
Answer by Baste · Dec 06, 2014 at 12:11 PM
I'd go with Quaternion.RotateTowards, as it's nice and compact. Example for W:
 if (Input.GetKeyDown (KeyCode.W)) 
 {
     Quaternion currentRotation = transform.rotation;
     Quaternion wantedRotation = Quaternion.Euler(0,0,90);
     transform.rotation = Quaternion.RotateTowards(currentRotation, wantedRotation, Time.deltaTime * rotateSpeed);
 }
Where rotateSpeed is a float variable just like speed. This will work for the majority of cases- lerping takes quite a bit of extra maths to get right if the distance you're going to rotate varies.
Answer by b1gry4n · Dec 06, 2014 at 11:32 AM
use transform.rotation : http://docs.unity3d.com/ScriptReference/Transform-rotation.html
convert to quaternion (or replace current euler values) : http://docs.unity3d.com/ScriptReference/Quaternion.Euler.html
slerp : http://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html
how to correctly lerp : http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly
difference between lerp and slerp : https://www.youtube.com/watch?v=uNHIPVOnt-Y#t=30
Your answer
 
 
             Follow this Question
Related Questions
How can I use lerp to rotate an object multiple times? 2 Answers
Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers
Quaternion.Slerp problem... 1 Answer
How to smoothly rotate an object on only two axes? 2 Answers
This can't be impossible or? Rotate (flip) smoothly from one point to endpoint on mouseclick. 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                