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 kvo1809 · Feb 06, 2020 at 12:37 PM · rotationrotate object

Rotate an object's z value to a certain an angle over time then rotate it back to the original angle

Hi, I'm having trouble understanding coroutines and rotation in general. I'm trying to make a 2D top down sprite attack animation similar to the one in this video... https://www.youtube.com/watch?v=yr_DLfoz8Jo

My goal: when a character inputs an attack key, the sprite would be rotated by a certain angle, pause for a very short time, then rotates back to the exact original angle.

My codes so far:

 public float maxTimer = 0.2f;
 public float waitTime = 0.1f;

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         StartCoroutine(RotateObject());
     }
 }
 

 IEnumerator RotateObject()
 {
     float timer = 0f;
     while (timer <= maxTimer)
     {
         transform.Rotate(Vector3.forward * Time.deltaTime * (40/maxTimer));
         timer += Time.deltaTime;
         yield return null;
     }
     yield return new WaitForSeconds(waitTime);
     timer = 0f;
     while (timer <= maxTimer)
     {
         transform.Rotate(Vector3.forward * Time.deltaTime * (-40/maxTimer));
         timer += Time.deltaTime;
         yield return null;
     }
 }

Now when I execute this code, the z value would slightly be different every time I hit space instead of returning to the original value of 0 (for example 1.1, -1.3, 0.5, -2.3, etc...). I also think there has to be a cleaner way of doing this. Note that the value 40 in the script is the angle value I want the sprite to be rotated by, and -40 to rotate it back to the original value. Any help would be appreciated!

Comment
Add comment · Show 7
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 vanphamnhatlan93 · Feb 06, 2020 at 04:28 PM 0
Share

sorry I am testing

avatar image vanphamnhatlan93 vanphamnhatlan93 · Feb 06, 2020 at 04:28 PM 0
Share

testing sorry testing

avatar image vanphamnhatlan93 vanphamnhatlan93 · Feb 06, 2020 at 04:28 PM 0
Share

testing sorry testing

Show more comments

1 Reply

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

Answer by unity_ek98vnTRplGj8Q · Feb 06, 2020 at 04:04 PM

I bet the reason you are seeing these variations is that you don't stop when timer == maxTimer, you stop on the first frame that its greater than maxTimer, and with variations on Time.deltaTime the amount of time you rotate forwards is going to differ slightly from the amount of time you rotate back. I personally recommend using Quaternion.Slerp since it will automatically clamp the rotation at the max value (and you will never overshoot). You can try this code out, I have not tested it but I think it will solve the issues you are having.

  IEnumerator RotateObject()
  {
      Quaternion startingRotation = transform.rotation;
      Quaternion targetRotation = startingRotation * Quaternion.Euler(0,0,40);
      float timer = 0f;
      while (timer <= maxTimer)
      {
          timer += Time.deltaTime;
          transform.rotation = Quaternion.Slerp(startingRotation, targetRotation, timer/maxTimer);
          yield return null;
      }
      yield return new WaitForSeconds(waitTime);
      timer = 0f;
      while (timer <= maxTimer)
      {
          timer += Time.deltaTime;
          transform.rotation = Quaternion.Slerp(targetRotation, startingRotation, timer/maxTimer);
          yield return null;
      }
  }


Comment
Add comment · Show 3 · 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 kvo1809 · Feb 06, 2020 at 06:40 PM 0
Share

Thank you for your reply. This solution fixed the issue. Now, there's a new problem. I also want the rotation to only happen when the object has been rotated back to the original angle, otherwise there's a bug when I execute RotateObject() again before the second while loop is finished running after the WaitForSeconds() function. The rotation value gets all messed up if I spam the space key multiple times before the whole rotation animation is finished. Therefore, I added a boolean variable to check by setting it to true right when it's executed and false after the second while loop is finished. I am not sure if this is an efficient way of solving this problem.

     public float maxTimer = 0.2f;
     public float waitTime = 0.1f;

     public bool isRotating;        

     void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             if (!isRotating)
             {
                 StartCoroutine(RotateObject());
             }
         }
     }

     IEnumerator RotateObject()
     {
         Quaternion startingRotation = transform.rotation;
         // Quaternion startingRotation = Quaternion.Euler(0,0,0);
         Quaternion targetRotation = startingRotation * Quaternion.Euler(0,0,40);
         isRotating = true;
         float timer = 0f;
         while (timer <= maxTimer)
         {
             timer += Time.deltaTime;
             transform.rotation = Quaternion.Slerp(startingRotation, targetRotation, timer/maxTimer);
             yield return null;
         }
         yield return new WaitForSeconds(waitTime);
         timer = 0f;
         while (timer <= maxTimer)
         {
             timer += Time.deltaTime; 
             transform.rotation = Quaternion.Slerp(targetRotation, startingRotation, timer/maxTimer);
             yield return null;
         }
         isRotating = false;
     }

Is this the right way to prevent this from happening?

avatar image unity_ek98vnTRplGj8Q kvo1809 · Feb 06, 2020 at 06:54 PM 1
Share

I don't see anything wrong with it, that's probably how I would have chosen to do it as well

avatar image kvo1809 unity_ek98vnTRplGj8Q · Feb 06, 2020 at 06:57 PM 0
Share

O$$anonymous$$ thank you

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

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

Related Questions

How to Rotate Object around point only 90 Degree. 1 Answer

Have an object continuously rotate towards another 1 Answer

How can I make a character look in a direction that's halfway between its direction of 2D movement and facing the camera? 1 Answer

How to limit the rotation of an object 2 Answers

Reletive rotation problems. (C#) 2 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