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 LukaKotar · Jun 28, 2012 at 12:28 PM · rotatesmoothdamplocaleulerangles

Problems rotating back to zero

Hi.

I recently created a script that made the player lean with middle mouse click and drag. The problem that appears, is that the player does not rotate back to zero (on the Z axis). It rotates closer to 0, but still far from it. I get a different number every time.

Here is the script causing this problem (sorry for the length):

 var leaning : boolean = false;
 var maximumLeft : float = -30;
 var maximumRight : float = 30;
 var currRotationZ : float = 0;
 var rotationSpeed : float = -5;
 var backToZeroSpeed : float = 2;
 var smoothDampVelocity : float;
 var maxSpeed : float = 5;
 var mouseLookObj1 : Transform;
 var mouseLookObj2 : Transform;
 
 function Update () {
     if(Input.GetMouseButton(2)){
         leaning = true;
         if(mouseLookObj1){
             mouseLookObj1.GetComponent(MouseLook).enabled = false;
         }
         if(mouseLookObj2){
             mouseLookObj2.GetComponent(MouseLook).enabled = false;
         }
         
         currRotationZ += Input.GetAxis("Mouse X") * rotationSpeed;
         currRotationZ = Mathf.Clamp(currRotationZ, maximumLeft, maximumRight);
         
         transform.localEulerAngles.z = currRotationZ;
     }if(!Input.GetMouseButton(2)){        
         leaning = false;
         if(mouseLookObj1){
             mouseLookObj1.GetComponent(MouseLook).enabled = true;
         }
         if(mouseLookObj2){
             mouseLookObj2.GetComponent(MouseLook).enabled = true;
         }    
     }if(!leaning){
         currRotationZ = Mathf.SmoothDamp(currRotationZ, 0, smoothDampVelocity, backToZeroSpeed);
         transform.rotation.z = Mathf.SmoothDamp(transform.rotation.z, 0, smoothDampVelocity, backToZeroSpeed);
     }
 }

Thanks,

Luka.

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
1
Best Answer

Answer by Wolfram · Jun 28, 2012 at 12:32 PM

Don't modify transform.rotation.z in your last line, that's a component of a Quaternion and has nothing to do with the Z axis. Use transform.localEulerAngles.z, as you did above.

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 LukaKotar · Jun 28, 2012 at 12:35 PM 0
Share

Thanks! I don't know what I was thinking... It works like a charm now!

avatar image
1

Answer by aldonaletto · Jun 28, 2012 at 12:43 PM

You must not change transform.rotation.z directly like you're doing in the last line! transform.rotation is a quaternion, and its XYZ components are not the fancy angles you see in the field Rotation in the Inspector - those are actually the localEulerAngles.
Maybe changing transform.rotation.z in the last line to transform.localEulerAngles.z makes it work - but not so well, because setting localEulerAngles individually may produce "rotation leakage", and after some time your object is tilted at weird angles.

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

6 People are following this question.

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

Related Questions

Wheels Rotate or Turn, but not both 1 Answer

Problem with using Rotate and localEulerAngles... 1 Answer

localEulerAngles don't seem to work in an equasion 3 Answers

Camera rotation around player while following. 6 Answers

localEulerAngles.x rotates as in world space 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