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 /
  • Help Room /
avatar image
0
Question by umurcg · Dec 25, 2016 at 09:21 AM · rotationquaternionrotation axis

How can I fix rotation of Z axis to 0?

Hello,

I am trying to right a bird controller.

In this script I am rotating object on Vector.up axis.

Also during elevation I rotate forward axis of object around transform.right axis to make birds head look towards upward or downward. (I limit that rotation with 45 degree)

In theory in shouldn't mess with Z axis (transform.right) of object. It should stay same. But after I run script for a little time, orientation of bird starts to breaking down.

Here is the script:

     void Update () {
 
         float hor = Input.GetAxis("Horizontal");
         float ver = Input.GetAxis("Vertical");
         float elev = Input.GetAxis("Elevation");
         float xAngle = transform.rotation.eulerAngles.x;
  
 
         bool eliminateHeight = false;
         if ((xAngle > 0 && xAngle<180 && transform.position.y < minHeight) || (xAngle < 360 && xAngle > 180 && transform.position.y > maxHeight))
         {
             eliminateHeight = true;
         }
 
         float prevHeight = transform.position.y;
         cc.Move( -transform.forward*ver*speed);
 
         if (eliminateHeight)
         {
             transform.position =new Vector3(transform.position.x, prevHeight , transform.position.z);
         }
 
         transform.Rotate(Vector3.up*hor,Space.World);
 
   
 
         if (elev == 0)
         {
             if (xAngle < 180f)
             {
                 if (xAngle > 1f)
                 {
                     transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation * Quaternion.Euler(-1, 0, 0), Time.deltaTime * 40);
                 }
                 else if (xAngle > 0)
                 {
                     transform.rotation = transform.rotation * Quaternion.AngleAxis(-xAngle, transform.right);
                 }
             }
             else
             {
                 if (xAngle < 359)
                 {
                     transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation * Quaternion.Euler(1, 0, 0), Time.deltaTime * 40);
                 }
                 else if (xAngle < 360)
                 {
                     transform.rotation = transform.rotation * Quaternion.AngleAxis((360 - xAngle), transform.right);
                 }
 
             }
 
         } else 
         {
             if(  ((xAngle>360-45||xAngle==0) || xAngle<45) || ((xAngle < 360 - 45 && xAngle>180 && elev < 0) || (xAngle > 45 && xAngle < 180 && elev > 0)))   
             transform.Rotate(-elev, 0, 0);
 
         }
         
     }


So I decided to write a if condition that checks z axis rotation and when z axis is not 0 it should set it to 0. But I couldn't manage to set only z axis to zero.

Here is the if condition

         if (zAngle != 0)
         {
             print("fixing z");
 
             if (zAngle > 180)
             {
                 transform.rotation = Quaternion.AngleAxis(-(360 - zAngle), transform.forward);
             }else
             {
                 transform.rotation = Quaternion.AngleAxis(-zAngle, transform.forward);
             }
           
 
         }

How can i set z axis rotation to 0 without altering other axes.

Edit: I fount the reason of why z axis rotation is changes. It is due to obstacles that bird encounter during its flight.

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

0 Replies

· Add your reply
  • Sort: 

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

96 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

Related Questions

Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers

Quaternions rotation stuck 0 Answers

Assign an object rotation along a single axis from other object's rotation. 0 Answers

How to have objects rotate around a given point that are scripted to face the player 1 Answer

Rotate a GameObject a fixed amount on key press struggles. Stops at 180. 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