Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 PixelSmash · Apr 18, 2014 at 10:52 AM · rotationquaternioneulerangleseulerlocaleulerangles

Fixing the rotation around a particular axis using Quaternions?

I have a problem. I have a gameobject and I want to set its rotation around it's local Z axis (blue) to some value (lets say 0) whilst preserving it's rotation about its local X and Y axis (red and green respectively).

The problem is that setting transform.localEulerAngles.z to 0 is not sufficient. Often changing the rotation around the local Z axis is done by modifying localEulerAngles.y, not localEulerAngles.z. Thus setting localEulerAngles.z to 0 does not necessarily set the rotation around the Z axis to 0. This is presumably because of the way Quaternions work where one Quaternion represents several Euler angles.

I've included a sample project to illustrate my problem, a script sets two gameobjects local Z Euler angles to 0 and yet they both have differing rotations around their local Z axis

Setting my transforms rotation around its local Z axis to a fixed value whilst preserving the other axis rotations is the last hurdle I have to overcome before I can get my game prototype out of the way. I've been stuck on it for a week and a half and I'm pretty much all dried up on the ideas front so any help would be really appreciated.

Thanks

EDIT: It was mentioned in the comments (by the Unity Answers legend that is robertbu, no less) that I should upload a more complete project which more accurately summarises my problem. I've just finished that and attached it below along with a more extensive description of the problem in the RTF/TXT files included in the package.

Thanks guys, it truly is much appreciated :)

New Demo Package

testprojectpackage.zip (412.5 kB)
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 KevLoughrey · Apr 18, 2014 at 12:30 PM 0
Share

Not sure if it will work for you, but if I remember correctly, you can modify the euler angles of the object via a quaternion using:

transform.rotation.eulerAngles

avatar image PixelSmash · Apr 18, 2014 at 01:35 PM 0
Share

Unfortunately transform.rotation.eulerAngles (or rotation.localEulerAngles) suffers from the same problem as conventional eulerAngles. What I really need is a method using Quaternion.AngleAxis, Quaternion.ToAngleAxis or something to do it. Thanks for the help though, much appreciated!

avatar image robertbu · Apr 18, 2014 at 02:21 PM 0
Share

I know I was the one that suggested a project, but the project you attached does not really help me understand how you are rotating the object or what you consider correct. As you know you can get a rotation on the 'z' axis (measure by eulerAngles), without ever rotating on the 'z' axis. And you can rotate on the 'z' axis, but the eulerAngles value doesn't match the rotation you input. Over the last year of answering rotation question, I come up with a "bag of tricks" as solutions to rotation problems. Often the solution is very different than expected. Things like projecting points on planes, viewing things a vector directions rather than angles, and isolating rotations into separate game objects. But in order to apply them I need a very clear idea of how you are doing the rotations and what you consider correct. And I don't for your question. If you are the one doing the rotation using the Transform, then my first suggestion is to use empty game objects and isolate the 'z' rotation to its own game object. Then your localRotation will not be impacted by other rotations.

avatar image PixelSmash · Apr 18, 2014 at 10:44 PM 0
Share

@robertbu Thanks, for your detailed post, it's awesome. Isolating a z rotation using a parent (or a separate transform) was something that I've been looking at over the past couple of days but so far I've had no luck. Perhaps it's something that I should really look into again.

I've thought about what you said with regards to having a clear idea of what the rotations are being used for and I think you're correct. I think when dealing with something as complex as Quaternions it was pretty optimistic of me (or perhaps just stupid) to think distilling my project to something as simple as my first post would offer me a decent solution.

I've now broken out the relevant section of my project, along with documentation, into a new Unity package which I've attached to the first post to hopefully make things clearer for everyone :).

Anyway, thanks again for the continued responses. It's a great help when trying to get your head around something as mathematically challenging (well, to me anyway) as Quaternions!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PixelSmash · Apr 20, 2014 at 10:40 AM

I ended up sorting it by using Math to get the rotation around the z axis and then using the inbuilt "RotateAround" method:

 Vector3 originalUp = transform.up;
 originalAngle = (Mathf.Atan2(originalUp.x, originalUp.z) * Mathf.Rad2Deg) + 180.0f;
 transform.RotateAround(transform.position,transform.forward, -originalAngle);
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
avatar image
0

Answer by exvalid · Feb 20, 2018 at 10:13 AM

is original angle a float or vector? thanks. Would this help to solve the Local Y not rotating Local Y axis?

this is whats stalling my current project.

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

22 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

Related Questions

Object makes random jumps in rotation 1 Answer

Trying to tween the rotation of a cube in x and z and having trouble 2 Answers

Complicated Rotation Issue 1 Answer

Can't Accurately Read Rotations 0 Answers

Is there no way to get reliable angles from a rigidbody? 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