Smooth 90 degree rotation on keypress
Hi Unity Anwsers,
I'm trying to make a C# script that'll rotate a GameObject 90 degress og -90 degress on the y-axis over 1 second when I press a button. I've searched around, but I can't find anything that works.
So far, I've made a float that holds rotation, and set it to 90f. Then I've put a transform.Rotate in my 'update' function when I press the keys 'e' or 'q' However, this turns the object immediately and not over time - can anybody help med add the time to my code?
 public float rotation = 90f;
 void Update () {
     if (Input.GetKeyDown ("e")) {
         transform.Rotate (0, rotation, 0);
     }
     if (Input.GetKeyDown ("q")) {
         transform.Rotate (0, -(rotation), 0);
     }
 }
 
              There are a few ways to accomplish this.
I would recommend you checking out Quaternion.Lerp()
or
Trying to rotate it based on Time.deltaTime() like this:
 void Update()
 {
     if (Input.Get$$anonymous$$eyDown ("e")) {
         transform.Rotate(0, rotation * Time.deltaTime, 0);
     }
     if (Input.Get$$anonymous$$eyDown ("q")) {
         transform.Rotate(0, -(rotation) * Time.deltaTime, 0);
     }
 }
 
                 Thanks @Nomabond,
I'm really, really new to C# and Unity, so I still need some help setting it up. What do I do with 'Transform from' and 'Transform to' to make it spin 90 degress on the keypress?
 public Transform from;
 public Transform to;
 public float rotation = 90f;
 void Update () {
     if (Input.Get$$anonymous$$eyDown ("e")) {
         Quaternion.Lerp(from.rotation, to.rotation, Time.time * rotation);
     }
                 No need to reinvent the wheel (even if you want smooth rotation!)
http://answers.unity3d.com/questions/672456/rotate-an-object-a-set-angle-over-time-c.html
That co-routine will do it, you don't even need it that complex but copy and paste it and send it the values 90 or -90 and 1 for the duration.
Answer by Rullaghn · Jun 13, 2016 at 10:25 PM
Thank you so much @Mmmpies.
That did the trick! My final code ended up looking like this:
     IEnumerator RotateMe(Vector3 byAngles, float inTime) 
     {    var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for(var t = 0f; t < 1; t += Time.deltaTime/inTime) {
             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
             yield return null;
         }
     }
 void Update () {
     if(Input.GetKeyDown("e")){
     StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
     }
     if(Input.GetKeyDown("q")){
     StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
     }
   }
 }
 
              Ive spent the last week trying to get this exact thing to work. Ive "almost got the answer" like ten times. This is the smoothest.
I think it could be edited a little to both normalize the rotation prevent an object from rotating in the middle of a rotation.
Your comment helped me build the solution that does this very thing. Thanks! :D
Hi I used this code to make object turn on its own. And created a code. And it didn't work (obviously). Would you please, look at it and tell me what I am doing wrong?
 bool rotated = false;
     
     void Update()
     {
 
         if (!rotated)
         {
             StartCoroutine(Wait());
             StartCoroutine(RotateForward(Vector3.forward * 45, 0.8f));
         }
         if (rotated)
         {
             StartCoroutine(Wait());
             StartCoroutine(RotateBackward(Vector3.back * 45, 0.8f));
         }
         
     }
     IEnumerator RotateForward(Vector3 byAngles, float inTime)
     {
         
         var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
         {
             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
             yield return null;
         }
         rotated = true;
     }
     IEnumerator RotateBackward(Vector3 byAngles, float inTime)
     {
         
         var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
         {
             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
             yield return null;
         }
         rotated = false;
     }
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(2);
     }
                 Answer by bobthenob · Jan 04 at 05:44 PM
i know this an old post but it helped me and will help others i altered it a bit to stop you rotating till it had finished or you could get odd angles
 bool rotating;
     IEnumerator RotateMe(Vector3 byAngles, float inTime)
     {
         var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for (var t = 0f; t <= 1; t += Time.deltaTime / inTime)
         {
             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
           
             yield return null;
         }
 
         transform.rotation = toAngle;
         rotating = false;
     }
     void Update()
     {
 
    
 
 
             if (Input.GetKeyDown("e") && !rotating)
             {
                 rotating = true;
                 StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
             }
             if (Input.GetKeyDown("q") && !rotating)
             {
                 rotating = true;
                 StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
             }
 
    
 
 
     }
 
              Answer by scortillion · Jan 21, 2017 at 02:53 PM
I noticed as I run this code the object (cube in my case) doesn't rotate a complete 90 degrees. It falls short on each key press. Any idea why?
The for-loop should be checking for t <= 1 ins$$anonymous$$d of t < 1, I believe.
Add transform.rotation = toAngle; after the for-loop.
Your answer
 
             Follow this Question
Related Questions
Why raycast2d not work? 0 Answers
Weird rotation when using Slerp!!? 3 Answers
Swapping Right handed Quaternions to left handed from streaming data. 1 Answer
How to limit only one axis rotation? c# 1 Answer
Rotating camera not working? 0 Answers