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 CB-TV · Nov 16, 2014 at 12:29 PM · vector3quaterniontransform.rotationtransform.rotate

Rotating an Object in Update Function

I originally was using: transform.Rotate But it rotated the object...forever. I want to rotate my object 90 degrees left and then STOP the rotation. But transform.Rotate just continued it because it was in an Update function. How can I stop the rotation at 90 degrees. Do I need to use a Quaternion? I've tried learning about Quaternions on Unity Scripting API but can't seem to get my head around them - they seem more complicated than what I want to do. Anyway, thank you if you know the answer to this!

Comment
Add comment · Show 2
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 b1gry4n · Nov 16, 2014 at 02:51 PM 0
Share

http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html

 from = current rotation (transform.rotation)
 to = desired 

http://docs.unity3d.com/ScriptReference/Quaternion.Euler.html

 Quaternion desiredRotation = Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.eulerAngles.y + 90, transform.eulerAngles.z);
 
 transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, overTime);



http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

avatar image CB-TV · Nov 17, 2014 at 04:06 PM 0
Share

Thanks for that!

1 Reply

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

Answer by Scribe · Nov 16, 2014 at 02:25 PM

How about using a coroutine, this is a rotate around method that will stop after the given number of degrees has been achieved:

 bool isRotating = false;
 public Vector3 this_Axis;
 public float deg;
 public float time;
 
 void Update () {
     if(Input.GetKey(KeyCode.Q) && !isRotating){
         StartCoroutine(RotateAround(this_Axis, deg, time));
     }
 }
 
 IEnumerator RotateAround(Vector3 axis, float degrees, float duration){
     if(axis == Vector3.zero || degrees == 0){
         return true;
     }
     isRotating = true;
     float d = 0;
     Quaternion q;
     Quaternion startRot = transform.rotation;
     float progress = 0;    
     while(progress <= 1){
         d = Mathf.Lerp(0, degrees, progress);
         q = Quaternion.AngleAxis(d, axis);
         transform.rotation = startRot*q;
         progress += Time.deltaTime/duration;
         yield return null;
     }
     transform.rotation = startRot*Quaternion.AngleAxis(degrees, axis);
     isRotating = false;
 }


Hope that helps!

Scribe

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 CB-TV · Nov 16, 2014 at 07:23 PM 0
Share

Thank you! But agob1gry4n did seem to give a much simpler answer but I'll definitely give coroutine's a go!

avatar image Scribe · Nov 16, 2014 at 07:36 PM 0
Share

It is simpler, I can't check at the moment but I think the difference is ig you want to rotate 360 degrees around an axis, this method will do the rotation where as lerp will think the start and end rotations are the same and therefore won't rotate! But that might be desired behaviour :) thanks for marking as answered

avatar image CB-TV · Nov 16, 2014 at 09:15 PM 0
Share

Cool, I'll try it!

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

28 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

Related Questions

Adding quaternions using a seperate axis 0 Answers

Aiming to gameobject 2 Answers

How to use a negative value when MathF.Clamping a eulerAngle? 1 Answer

transform.forward wobbles about instead of rolling 1 Answer

Rotate Issue 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