Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Cursed-form · Oct 03, 2019 at 10:57 AM · rotationanglechangerotate objectsmoothly

SOLVED Rotate object smoothly to calculated angle on key press (and change the target angle during rotating movement)

Hey!

I know this question has been asked earlier but the given answers haven't worked perfectly for me. So, I have a cube (=mainMenuCube) which I want to rotate smoothly towards the calculated angle on Y axis every key press. Pressing A subtracts 90 degrees from this target angle while pressing D adds 90 degrees to the angle. At the moment, my code looks like this:

       GameObject mainMenuCube;
       public float newYAngle;
       public float oldYAngle;


        void Start()
         {
            mainMenuCube = GameObject.Find("main_menu_cube");
         }
     
         void Update()
         {
            if (Input.GetKeyDown(KeyCode.A))
             {
                 newYAngle = oldYAngle - 90f;
                 StartCoroutine(Rotate(newYAngle));
                 oldYAngle = newYAngle;
              }
     
            if (Input.GetKeyDown(KeyCode.D))
             {
                 newYAngle = oldYAngle + 90f;
                 StartCoroutine(Rotate(newYAngle));
                 oldYAngle = newYAngle;
              }
           }
     
         IEnumerator Rotate(float targetAngle)
         {
             while (mainMenuCube.transform.rotation.y != targetAngle)
             {
                 mainMenuCube.transform.rotation = Quaternion.Slerp(mainMenuCube.transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
                 yield return null;
             }
             mainMenuCube.transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
             yield return null;
         }

However, the code works only once; when I press A or D for the second time, the cube stops before reaching it's destination and starts jittering instead. But I can see that the variables called "newYAngle" and "oldYAngle" are updating correctly in the Inspector and I'm not getting any error messages.

Notice that I want to be able to change the target angle even when the cube is still rotating, not only when it has stopped. In other words, if I press A once, the target angle will be -90 degrees but if I press A again before the previous rotation movement has been finished, the target angle switches to -180 degrees and the cube won't stop at -90 degrees.

By the way, I'm not an experienced coder so try to be clear with your possible answers. And sorry if my English is bad; it's not my mother language.

Comment
Add comment
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

1 Reply

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

Answer by Cursed-form · Oct 03, 2019 at 11:11 AM

Wait! I actually managed to sort it out myself right after posting this question. XD All I needed to do was to add StopAllCoroutines();. Here's the final code if someone is interested in coding something similar:

        GameObject mainMenuCube;
        public float newYAngle;
        public float oldYAngle;

          void Start()
          {
             mainMenuCube = GameObject.Find("main_menu_cube");
          }

          void Update()
          {
             if (Input.GetKeyDown(KeyCode.A))
              {
                  StopAllCoroutines();
                  newYAngle = oldYAngle - 90f;
                  StartCoroutine(Rotate(newYAngle));
                  oldYAngle = newYAngle;
               }
      
             if (Input.GetKeyDown(KeyCode.D))
              {
                  StopAllCoroutines();
                  newYAngle = oldYAngle + 90f;
                  StartCoroutine(Rotate(newYAngle));
                  oldYAngle = newYAngle;
               }
            }
      
          IEnumerator Rotate(float targetAngle)
          {
              while (mainMenuCube.transform.rotation.y != targetAngle)
              {
                  mainMenuCube.transform.rotation = Quaternion.Slerp(mainMenuCube.transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
                  yield return null;
              }
              mainMenuCube.transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
              yield return null;
          }     

Use it wisely. ;)

By the way, this script is NOT attached to the rotating cube itself, which is why I have to find it in the Start function. If your script is part of the object to be rotated, the code would be slightly different:

            public float newYAngle;
            public float oldYAngle;
 
              void Update()
              {
                 if (Input.GetKeyDown(KeyCode.A))
                  {
                      StopAllCoroutines();
                      newYAngle = oldYAngle - 90f;
                      StartCoroutine(Rotate(newYAngle));
                      oldYAngle = newYAngle;
                   }
          
                 if (Input.GetKeyDown(KeyCode.D))
                  {
                      StopAllCoroutines();
                      newYAngle = oldYAngle + 90f;
                      StartCoroutine(Rotate(newYAngle));
                      oldYAngle = newYAngle;
                   }
                }
          
              IEnumerator Rotate(float targetAngle)
              {
                  while (transform.rotation.y != targetAngle)
                  {
                     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
                      yield return null;
                  }
                  transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
                  yield return null;
              }     
Comment
Add comment · 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

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

146 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot rotate an object around the center 5 Answers

Rotating 2D sprite to match surface. 0 Answers

Rotating an object in a 3D grid 2 Answers

Quaternion and rotation angles 2 Answers

Rotate cube in two direction not working 1 Answer


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