Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by JammyGeeza · Mar 25, 2014 at 04:22 AM · rotateeuleranglesquaternions

Rotate an object a set angle over time? (C#)

Hello!

I have an object that I want to rotate 20 degrees, but I want it to rotate ONLY 20 degrees over a few seconds. I just cannot get it to work!

I've tried things like:

 object.transform.Rotate(Vector3.right * (20 * Time.deltaTime));

But from what I understand, this just rotates it by 20 degrees every second and doesn't stop at 20. I tried using an "if (object.transform.rotation <=20)" with the above code but that didn't stop it either.

I have my object moving forwards in the z-axis with Lerp by using:

 object.transform.position = new Vector3(object.transform.position.x, object.transform.position.y, Mathf.Lerp(object.transform.position.z, (destination.transform.position.z+10), Time.deltaTime));

That seems to work fine when moving an object a certain distance, but I tried to implement the same idea with rotation and I just can't do it. I get quite a lot of errors about Quaternions and Vectors can't be converted etc.

I don't fully understand Quaternions but I have a feeling I have to use them.

Any help would be much appreciated! Thanks alot!

EDIT: To rephrase the question, I don't have to be able to set how many seconds it takes to rotate. I just want it to gradually rotate, but stop after a certain amount of degrees.

Everything I try seems to just make it continuously rotate, but I just want it to stop rotating after a set amount of degrees.

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
10

Answer by whydoidoit · Mar 25, 2014 at 04:34 AM

The answer is to use a coroutine or a condition in your Update to uses a state to decide whether you should be rotating or not.

Coroutines are easy for 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.Lerp(fromAngle, toAngle, t);
                yield return null;
           }
      }

Then to rotate by 20 degrees in 5 seconds - do this once when you want it to start

     StartCoroutine(RotateMe(Vector3.right * 20, 5));
Comment
Add comment · Show 20 · 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 JammyGeeza · Mar 25, 2014 at 02:10 PM 0
Share

Thanks for your quick reply!

I've put it into my code, but it didn't work. It doesn't recognise StartCoroutine as a method.

I removed StartCoroutine so it just left me with Rotate$$anonymous$$e(Vector3.right * 20, 5); but still no luck!

Is it javascript? If so, I unfortunately have to have this in C# as it's an assignment! :(

avatar image Lo0NuhtiK · Mar 25, 2014 at 02:12 PM 0
Share

Dat do be C#. ...mixed JS lol

avatar image Lo0NuhtiK · Mar 25, 2014 at 02:20 PM 1
Share
 IEnumerator Rotate$$anonymous$$e(Vector3 byAngles, float inTime)
 {
    Quaternion fromAngle = transform.rotation ;
    Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + byAngles) ;
    for(float t = 0f ; t < 1f ; t += Time.deltaTime/inTime)
    {
       transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ;
       yield return null ;
    }
 }
 
 -----
 StartCoroutine(Rotate$$anonymous$$e(Vector3.right * 20f, 5f)) ;
avatar image warbywarbywarby Lo0NuhtiK · Apr 25, 2017 at 07:04 AM 0
Share

i likes this code a rubicks cube game i was making problem being is the 20F the angle ...... i changed the 20f to 90f, and the 5f ( seconds) to 1f.. for a quicker rotation over 90 degrees.... simple enough.. but the rotation isn't exactly 90 degrees it just around and rotates a random number each time., so over time the cube bits unaligned them self's over time , so if this was and defiantly the degrees this would have been a brilliant code. i'm new to all this so if ya know of a way to help me out id really appreciate it ...

avatar image VirtuallyHealthy Lo0NuhtiK · Jun 27, 2020 at 02:24 PM 0
Share

Why does this need to be done using a coroutine? Isn't that inefficient as coroutines require quite a bit of processing power?

Also I can't get it to rotate the exact angle using this code - its always a degree or so out =/

avatar image JammyGeeza · Mar 25, 2014 at 02:26 PM 0
Share

When I put it into my code, StartCoroutine is red and says:

"The name 'StartCoroutine' does not exist in the current context."

Do I need to add a 'Using' at the top of my code? Is there something I'm missing?

avatar image JammyGeeza · Mar 25, 2014 at 03:11 PM 1
Share

I'VE DONE IT! I used Quaternion.AngleAxis to get it to stop at 20 degrees! I floated these at the top:

 public float totalRotation = 0;
 public float rotationAmount = 20f;

Then I used Quaternion.AngleAxis to get it to stop at 20 degrees:

         if($$anonymous$$athf.Abs (totalRotation) < $$anonymous$$athf.Abs (rotationAmount)){
             float currentAngle = cube.transform.rotation.eulerAngles.x;
             cube.transform.rotation = Quaternion.AngleAxis(currentAngle + (Time.deltaTime * 5), Vector3.right);
             totalRotation += Time.deltaTime * 5;
         }
Show more comments

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

25 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

Related Questions

Multiple Cars not working 1 Answer

How to lock the Z axis so my Enemy just flips on the Y axis 1 Answer

Delay Problem, coule be 'OnMouseEnter'? 0 Answers

Rotating a model with increments 1 Answer

i would like to rotate an object from an other objects script 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