• 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
2
Question by Kith · May 30, 2011 at 02:32 AM · transformrotateanglestuckdegrees

Transform.Rotate() stuck at 90 and 270 degrees

Has anyone had t$$anonymous$$s problem? For some reason, when I use Transform.Rotate() around the x-axis (Vector3.right), the object that is rotating will invariably get stuck at 90 degrees and 270 degrees (Straight up and Straight down).

Anyone have any insight? Thanks!

     if (leftStick.position.y >= buffer) {
         transform.Rotate(Vector3.right * leftStick.position.y * Time.deltaTime * rotationSpeed, Space.World);
         
     }    
     
     if (leftStick.position.y <= -buffer) {
         transform.Rotate(Vector3.right * leftStick.position.y * Time.deltaTime * rotationSpeed);    
     }

leftStick is an iPhone Joystick, that ranges from -1 to 1 depending on where the user moves the joystick. If the user holds the joystick up, the s$$anonymous$$p moves down as it should, and vice versa. When the s$$anonymous$$p's x-rotation reaches 90 or 270 though, it no longer can rotate up or down. It gets stuck.

Comment
Add comment · Show 4
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 Anxo · May 30, 2011 at 02:33 AM 0
Share

link your code

avatar image Kith · May 30, 2011 at 02:52 AM 0
Share

I updated the OP. Thanks. Also, ignore that Space.World. That was me trying to see if that would make a difference, but it didn't.

avatar image aldonaletto · May 30, 2011 at 03:36 AM 0
Share

I don't know if it can help, but I adapted this script to my PC Unity and it worked fine, rotating an object without any restriction. My object was simple capsule, not a CharacterController, and had no rigidbody.

avatar image ocimum · Jul 20, 2015 at 11:18 AM 0
Share

have a look at @kungfooman answer, the extension wil calculate the new variable dependent on the value you want to set. Looks to me like the answer for this question.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Wolfram · May 30, 2011 at 03:44 AM

Do you need a rotation of 90 degrees or even >90? Or would a rotation limit of 85 degrees (resp.275 degrees) suffice?

Unity does some strange clamping/snapping for X rotations of/near 90/270 degrees, due to Gimbal lock problems. I t$$anonymous$$nk once the X-rotation exceeds 87 degrees, it will snap to some value, and trying to decrease that value again by small amounts (e.g., scaled by Time.deltaTime) will do not$$anonymous$$ng.

To prevent that from happening, you should limit your X rotation to, say, =275 degrees.

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 Owen-Reynolds · May 30, 2011 at 04:10 AM 0
Share

For a test, try setting rotation.EulerAngle.xyz to a public Vector3. You should see the snapping. A brute force fix is to declare a separate float Angle;, have joysticks change that, and set directly to that angle each frame, using quaternion.Eulerangle.

avatar image
2
Wiki

Answer by kungfooman · Mar 03, 2015 at 12:51 PM

I wrote t$$anonymous$$s extension method to provide a "safe" x, based on the fact that rotations from -90 to 90 work fine with no Gimbal Lock. Thats exactly the area an FPS Player would be able to look (-90 is up, 90 is down). An x value of 120 e.g. would be translated to -60, w$$anonymous$$ch will work then. It could be extended to provide full 360°, by detecting the gimbal lock and add 180 to y then.

 using UnityEngine;

 public static class ExtensionVector3
 {
     public static float CalcEulerSafeX(float x)
     {
         if (x >= -90 && x <= 90)
             return x;
         x = x % 180;
         if (x > 0)
             x -= 180;
         else
             x += 180;
         return x;
     }
     public static Vector3 EulerSafeX(t$$anonymous$$s Vector3 eulerAngles)
     {
         eulerAngles.x = CalcEulerSafeX(eulerAngles.x);
         return eulerAngles;
     }
 }

Usage in a simple Mouse script:

 using UnityEngine;
 using System.Collections;

 public class PlayerMouse : MonoBehaviour {
     public float x = 0;
     public float y = 0;
     public float sens = 2.5f;

     void Start () {
         // either only call once to save initial rotation
         x = transform.localRotation.eulerAngles.EulerSafeX().x;
         y = transform.localRotation.eulerAngles.y;
     }

     void Update()
     {
         if (!Screen.lockCursor)
             return;

         x -= Input.GetAxis("Mouse Y") * sens;
         y += Input.GetAxis("Mouse X") * sens;
         x = Mathf.Clamp(x, -90, 90);
         
         // or handle publically changed x values always
         transform.localRotation = Quaternion.Euler(ExtensionVector3.CalcEulerSafeX(x), y, 0);
     }
 }
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 ocimum · Jul 20, 2015 at 11:17 AM 0
Share

This is exatly what I was looking for. This problem appears often, doesn't matter if you want to change the x parameter or another one. This is what I recognised, should be the answer to the question, but isn't there a possibility to use a method build in, to solve this problem?

avatar image
1

Answer by ChrisR · Dec 10, 2012 at 07:50 AM

Wow I've seen references to t$$anonymous$$s issue dating back to '07. Same for me now using Unity 4... might be time for a bug fix:) Ok t$$anonymous$$s was my fix to get it outside of the 3 degree dead stick range. You get a slight jump when passing that range but it's better then limiting your functionality. Also Note that it locks at 90 and 270 when doing a transform.localEulerAngles.x = transform.localEulerAngles.x +1. I'm truly scratc$$anonymous$$ng my head why t$$anonymous$$s bug still exists.

if (Input.GetKey("s")){ transform.Rotate(Vector3.right Time.deltaTime -50); if (transform.localEulerAngles.x==270) transform.localEulerAngles.x = 267; if (transform.localEulerAngles.x==90) transform.localEulerAngles.x = 87; }

if (Input.GetKey("w")){ transform.Rotate(Vector3.left Time.deltaTime -50); if (transform.localEulerAngles.x==270) transform.localEulerAngles.x = 273; if (transform.localEulerAngles.x==90) transform.localEulerAngles.x = 93; }

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 Eric5h5 · Dec 10, 2012 at 08:30 AM 0
Share

It's not a bug; Unity uses quaternions internally for rotations. Checking one element of eulerAngles is generally not a good idea since the conversion from quaternions to euler angles can be done in more than one valid way (e.g, 0,0,0 is the same as 180,180,180), and there's no guarantee that the conversion will turn out the same consistently.

There's a reason the docs tell you not to increment eulerAngles. The best way is to track rotation yourself like the standard MouseLook script does.

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

7 People are following this question.

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

Related Questions

Rotate 90 over time on mouseDown 2 Answers

Rotate object in a set amount of time 1 Answer

Rotate 90 degrees over time with Parabolic Easing. 1 Answer

Gradually Rotate Character 90 Degrees 2 Answers

rotate the object only 360degree for once 3 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