- Home /

**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 :)

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`

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!

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.

@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!

**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);
```

**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.

### Your answer

### 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

### Related Questions

Is there no way to get reliable angles from a rigidbody? 2 Answers

trouble with Quaternion rotation in 360 axis degree 0 Answers

Need help with storing Rotation in a Quaternion 1 Answer

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