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 :slight_smile:

[25407-testprojectpackage.zip|25407]

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

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.