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 sadsa · Oct 02, 2015 at 11:55 AM · rotationrotate objecteuleranglesrotatearoundgimbal-lock

Rotation Problem - Simulating Gimbal lock

I'm Having a problem with rotations. I've done some research about this problem but it seems that everyone solves it with rotateArround. I'm trying to replicate the rotations of a SpaceShip on a graphic representation. For that, I have to replicate its rotation on X, Y and Z on another object. My object 1 is a gray cube and object two is a Colored cube with other cubes in it.

The red represents X. The green represents y. The blue represents z.

When I rotate cube 1 on X axis, the cube two red (and its children) should rotate the same amount. The Y and Z work ok but the X is big problem. alt text alt text

In the end - the lowest cube on the hierarchy should be on the same position that the reference object.

The problems are: -x only rotates between [270, 90] and after that the cube rotates in a weird way. - when I copy the rotation values using quaternions the W recalculates automatically and I can't copy on only one axis.

The CODE:

             if (xBool) {
                 transform.localEulerAngles = new Vector3 (originalObj.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
             }
             if (yBool) {
                 transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, originalObj.localEulerAngles.y, transform.localEulerAngles.z);
             }
             if (zBool) {
                 transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, transform.localEulerAngles.y, originalObj.localEulerAngles.z);
             }    

Can someone help me?

thank you for your time!

cubes-3.png (30.1 kB)
cubes.png (25.6 kB)
Comment
Add comment · Show 5
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 meat5000 ♦ · Oct 02, 2015 at 10:38 AM 0
Share

Some Unity rotation functions and angle based stuff tend to work up to 180 degrees. Working in Euler in Unity is a dangerous thing in general and you should consider contructing a quaternion out of your rotational information. Look at Quaternions in the Scripting API. You will find you can modify a quaternion using euler if you need.

Unity sometimes cant decide if its 90 or -270 etc. You need to add some data conditioning to your degrees before you use the data.

 if(degrees < 0)  //This kind of thing.
 { degrees += 360; }

Sorry if Im of no help whatsoever.

avatar image sadsa meat5000 ♦ · Oct 02, 2015 at 03:05 PM 0
Share

Thank you for your help! I tried to make some degree ajustments make it all turns out in a big unprecise mess.

If I rotate the 360 it turns out the same.

The problem with the rotation is that the original object can rotate normally but when it passes the 90 degrees it changes it reference (changes y and z to 180) and the "replica" maintains its original reference

avatar image _Gkxd meat5000 ♦ · Oct 03, 2015 at 03:57 AM 0
Share

@meat5000
Euler angles are fine for representing rotations; there's nothing that "dangerous" about them. Adding 360 to any angle in the Euler angle won't change anything. Unity does that to keep Euler angles within a certain range. Euler angles only start to fail when you try to interpolate them the way you would vectors.

For example, let's say you take a vector pointing in the positive x axis, rotate it about the z axis by 90 degrees so that it points up. At this point, you can do any rotation about the y axis and the vector would be the same (gimbal lock). That is, the Euler angle (0, 0, 90) is the same as the Euler angle (0, 180, 90) and is the same as (0, y, 90) for any value of y.

What would happen if you wanted to interpolate the Euler angle (0, y, 90) with (0, 0, 0)? For (0, y, 90), the vector in the above example points up, and for (0, 0, 0) the vector points right. In the "correct" interpolation, at the half way point, the Euler angle of the vector should be (0, 0, 45). However, if you actually interpolate the two Euler angles, you would get the Euler angle (0, y/2, 45) ins$$anonymous$$d, which will only give the "correct" result when y = 0.

To obtain the correct interpolation, you would need to do a bit more work. If we have two vectors and we want to rotate one to the other, we can obtain the angle between the two vectors by dotting them. Then, we rotate the first vector by a fraction of that angle, about the axis that is perpendicular to the two vectors (obtained using the cross product). You don't actually have to do this, since using quaternions will handle this.

@sadsa
I think your problem might be caused by using localEulerAngles ins$$anonymous$$d of eulerAngles. If you rotate the parent, the children automatically rotate with it. If you set the local Euler angles of the children to the parent's Euler angles, then the children will perform the parent's rotation twice: once from the parenting, and once from setting the local Euler angles.

avatar image sadsa _Gkxd · Oct 05, 2015 at 01:59 PM 0
Share

You might be right. There must be a problem with the hierarchy but I tried every combination of localEulerAngles, eulerAngles.. and the Z rotation works fine, but the other two do not.. I deleted the hierarchy and done it all again.

alt text

     void Update () {
 
         if(x){
             transform.rotation = Quaternion.Euler(targetCube.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
 
         }
         if(y){
             transform.localRotation = Quaternion.Euler(transform.eulerAngles.x, targetCube.eulerAngles.y, transform.eulerAngles.z);
         }
 
         if(z){
             transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, targetCube.eulerAngles.z);
         }
     }
 }

with the hierarchy : alt text

I think this is giving me the global rotation of the target ins$$anonymous$$d of the local rotation. The problem should be here: targetCube.eulerAngles.x

how can I correct this?

(THAN$$anonymous$$ YOU SO $$anonymous$$UCH FOR SPENDING THE TI$$anonymous$$E TO REPLY! and sorry for my bad english)

hierarchy.png (1.8 kB)
cubes-axis.png (48.1 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Oct 04, 2015 at 07:09 AM

Instead of editing the localEuler angles directly I believe you can avoid Gimbal Lock by using Unity's built in Euler function:

 transform.rotation = Quaternion.Euler(originalObj.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
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

7 People are following this question.

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

Related Questions

Why is this rotation not performed as expected? 1 Answer

rotation with eulerangles and raycasting. 1 Answer

Quaternion reset rotations didnt smooth 0 Answers

Rotate object based on set time. 2 Answers

Proper way to rotate an object? 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