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 /
avatar image
0
Question by VKBobyr · May 14, 2017 at 10:31 PM · rotationtransformquaternionlocalrotation

transform.localRotation resets after rotation

Hello! I've been trying to rotate a game object on its local x-axis for a while now, and have no idea what is wrong with my code. Whenever I look up or down, the objects rotates where its supposed to be, but seems to go back to its original (0,0,0) position during the next couple of frames. I used Debug.Log to check it's values, and it seems like even though in the editor the values change during rotation, when the Update function is executed, the rotation x-axis rotation of the object is always extremely close to 0.

When i replace "transform.localRotation.x" in the first line of the Update function with "transform.localEulerAngles," rotation works as desired, but up to 0 degrees. (Known issue with Eulers)

Could somebody please explain why "transform.localRotation.x" always returns "0"?

 public class PlayerRotator : MonoBehaviour {
         float flRotationSpeed = 150;
         float _xRotation;
 
 
     void Update () {
         _xRotation = Mathf.Clamp(transform.localRotation.x + (-Input.GetAxisRaw("Mouse Y") * flRotationSpeed * Time.deltaTime), -85, 85);
 
         Quaternion rotation = Quaternion.Euler(_xRotation,0,0);
 
 
         transform.localRotation = rotation;
     }
 }
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
0

Answer by toddisarockstar · May 18, 2017 at 12:53 AM

Euler angles do not except numbers greater than 360 or less than zero. it would be easier in this case to simply use transform.rotate but if you wanted to directly change the angle in eulers you would do it like this with a correction:

 float rot;
     float speed;
     void Start(){
         speed = 10;
         rot = transform.localEulerAngles.x;
     }
     
     void Update () {
         rot = rot -Input.GetAxisRaw ("Mouse Y") * speed * Time.deltaTime;
         if(rot>360f){rot-=360f;}
         if(rot<0f){rot+=360f;}
         
         transform.localEulerAngles = new Vector3(rot,transform.localEulerAngles.y,transform.localEulerAngles.z);
     }


in other words, your script is trying to push a rotation below zero. the PC still thinks its zero. a rotation 5 degrees below 0 would actually be 355. just like the hands of a clock passing 12.... there is no 13 O'clock. rotation numbers are represented 0 to 360. anyways, Quaterntons will regester numbers outside of that but eulers will not. but eulars are much easier to work with cause quaterntons are much more complicated. if you use transform.rotate, the friendly developers at unity consider all this for you in their function and you don't need to worry about all this sillyness when rotating an object. but its still good for you to know

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

Child versus Parent rotations 3 Answers

Boids for 2D 0 Answers

Instantiated Objects Point towards unknown point 0 Answers

How to use quaternion.lerp 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