Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Rullaghn · Jun 13, 2016 at 01:47 PM · c#rotationquaternion

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);
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nomabond · Jun 13, 2016 at 01:50 PM 1
Share

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);
     }
 }
avatar image Rullaghn · Jun 13, 2016 at 02:15 PM 0
Share

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);
     }
avatar image Mmmpies · Jun 13, 2016 at 08:31 PM 1
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

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));
     }
   }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aloften · Jun 16, 2019 at 12:10 AM 0
Share

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

avatar image Orange-Pixell · Apr 21, 2020 at 12:24 PM 0
Share

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);
     }

avatar image
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));
             }
 
    
 
 
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khoray · Jan 27 at 03:07 PM 0
Share

Works great. Thanks!

avatar image
0

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hammad-zahid · Aug 23, 2017 at 06:57 AM 0
Share

me too faced the same issue.

avatar image hammad-zahid · Aug 23, 2017 at 06:57 AM 0
Share

found any solution

avatar image BLourenco hammad-zahid · Sep 19, 2017 at 02:50 AM 0
Share

The for-loop should be checking for t <= 1 ins$$anonymous$$d of t < 1, I believe.

avatar image David_Rios hammad-zahid · Jan 02, 2018 at 05:22 PM 2
Share

Add transform.rotation = toAngle; after the for-loop.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges