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
0
Question by laskiapina2 · Dec 02, 2012 at 10:29 PM · rotationjavascriptdegrees

How to rotate object 180 degrees smoothly

I've tried googling my problem whole day and can't get it to work. The game is a sidescroller. I've got an aeroplane that flies and when it rotates its Z over it's supposed to rotate its X 180 degrees so the pilot is never upside down.

The problem is that it doesn't do it clean and sometimes the plane stops its rotation in the middle of the turning and messes up the directions.

If someone could tell me how to do it so the object rotates 180 degrees smoothly from 0 to 180 degrees it would be awesome.

Here's my code:

 if (Input.GetAxis ("Horizontal") < 0 || Input.GetAxis ("Horizontal") > 0) transform.Rotate(0,0,-1*Input.GetAxis ("Horizontal") * 150 * Time.deltaTime,Space.World);
 
 if (transform.rotation.eulerAngles.z > 90 && transform.rotation.eulerAngles.z < 110 && doRotate == 0) doRotate = 1;
 if (transform.rotation.eulerAngles.z > 250 && transform.rotation.eulerAngles.z < 270 && doRotate == 0) doRotate = 1;
 
 if (doRotate == 1) transform.Rotate(300*Time.deltaTime,0,0,Space.Self);
 if (doRotate == 1 && transform.rotation.eulerAngles.x < 180) {
 transform.rotation.eulerAngles.x = 0;
 doRotate = 0;
 }
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
0

Answer by Goodgab · Mar 16, 2013 at 06:24 PM

This is my code for rotating a plane when clicked 180 degrees. When clicked again, it rotates bakc 180 degrees.

 var rotateFront = false;
 var rotateBack = false;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     if (rotateFront)
         Reveal();
     if (rotateBack)
         Hide();
 }
 
 function OnMouseDown()
 {
     if(rotateFront)
     {
         rotateFront = false;
         rotateBack = true;
     }
     else if (rotateBack)
     {
         rotateFront = true;
         rotateBack = false;
     }
     else
         rotateFront = true;
     
 }
 
 function Reveal()
 {
     if (transform.rotation.y < 1)
         transform.Rotate(0,1,0);  //you can change axis to rotate and speed
 }
 
 function Hide()
 {
     if (transform.rotation.y > 0)
         transform.Rotate(0,-1,0);
 }

the Reveal() and Hide() functions do what you are asking for.

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

Answer by robertbu · Mar 16, 2013 at 07:28 PM

I can tell you what your problem is, but I cannot give example code without a better description of your situation (which way the camera is facing and a graphic of why the plane needs to be flipped). My guess is there is a solution without doing the flipping.

But as for the problem with the code above, for any given physical rotation of your object, there are multiple euler angle representations. If you rotate 180 degrees about the 'X' axis for example, reading back transform.eulerAngles.x (which you can do instead of transform.rotation.eulerAngles) may not produce a value of 180. Instead you may see a rotation on the z and y axex and a 0 rotation on 'x' axis (which would be the same physical rotation).

A solution I use is to keep my own Vector3 and set (but never read) the euler angles. Note you should always set all three euler angles together. Never set them independently.

So the code might look like:

 angle += 300 * Time.deltaTime;
 transform.eulerAngles = Vector3(angle, 0.0, 0.0);
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

10 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

Related Questions

2D Rotation Lerp Back To 0 Degrees, Please Help 2 Answers

Problem with oscillation 3 Answers

0-360 Y degree from Vector3.Angle 1 Answer

Move an object toward an angle in 2d space 0 Answers

Detecting the range between 2 angles 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