• 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 Erik · Mar 31, 2012 at 09:56 AM · rotations

Rotate object over time, 90 degrees at a time

I checked other similar questions and code examples, but I could not find something suitable to my situation.

I need to rotate an object (smoothly) by 90° at a time upon certain events (for example, hitting a key or clicking with a mouse button).

I have a function which does roate the object by 90°, but not smoothly:

transform.Rotate(0, 90, 0);

how can I make it rotate smoothly?

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

2 Replies

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

Answer by aldonaletto · Mar 31, 2012 at 11:16 AM

The easiest way is to use eulerAngles and a coroutine: calculate the desired new angle and fire a coroutine that will rotate automatically over time. To avoid other coroutines being fired while the first one is still running, you must set a boolean flag at the beginning, abort other calls while it's set and clear the flag upon return:

 var speed:float = 90.0; // degrees per second
 private var curEuler:Vector3;
 private var rotating:boolean = false;
 
 function Start(){
   curEuler = transform.eulerAngles;
 }
 
 function RotateAngle(angle: float){
   if (rotating) return; // ignore calls to RotateAngle while rotating
   rotating = true;  // set the flag
   var newAngle = curAngle.y+angle; // calculate the new angle
   while (curEuler.y < newAngle){
     // move a little step at constant speed to the new angle:
     curEuler.y = Mathf.MoveTowards(curEuler.y, newAngle, speed*Time.deltaTime);
     transform.eulerAngles = curEuler; // update the object's rotation...
     yield; // and let Unity free till the next frame
   }
   rotating = false;
 }

Just call the function RotateAngle(desiredAngle) and it will rotate the desired degrees automatically.

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 pabloromocl · Sep 09, 2013 at 04:00 PM 0
Share

This worked great for me.

I have a character that should turn around in 90 degrees just pressing any left or right axis.

The only thing that i gotta check out was that it didn't work for rotating to the other side or negative angles RotateAngle(-90), but is not hard to do:

change:

var newAngle = curAngle-angle;

while(curEuler.y > newAngle)

and that's it.

avatar image nath23725652 · Aug 31, 2014 at 02:14 AM 1
Share

what is newAngle, i tried using this script but it does not recognise it

avatar image WikiMalik · Apr 24, 2015 at 07:12 PM 0
Share

What is curAngle? Please explain its purpose because it's not defined anywhere

avatar image
1

Answer by Hybris · Mar 31, 2012 at 10:39 AM

transform.Rotate rotates the object at a speed. You could say:

 var RotSpeed = 2.0;

 transform.Rotate(0, Rotspeed, 0);
 if(transform.rotation.y >= 90){
    transform.rotation = 90;

 }

or just a little change if the code above lets the object behave weird, you could say:

 var rotated = false;

 if(!rotated){
    transform.Rotate(0, Rotspeed, 0);
 }

 if(transform.rotation.y >= 90){
    transform.rotation.y = 90;
    rotated = true;

 }
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 aldonaletto · Mar 31, 2012 at 10:55 AM 1
Share

transform.rotation is a quaternion, and its components are always between -1 and 1. You should use transform.eulerAngles, which is transform.rotation converted to/from the 3 axes Euler angles representation.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Can I use Quaternions to get from one point to another? 1 Answer

Mecanim adds rotation to animation clip during animation apply 0 Answers

Endless racer - positioning of track segments 0 Answers

Quaternions rotating left and right, not forward and back 1 Answer

Doing a Star Fox Style barrel roll? how best to rotate a gameobject over a period of time and have it return it's original rotation? 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