After multiple transform.Rotate, align with an axis?

After multiple rotations about the Z axis such as:

transform.Rotate(0, 0, rotateMagnitude);

is it easily possible to make transform align with the Y axis? I.E. if transform started off being aligned with the Y axis, is there an easy way to get the transform back to its initial alignment?

The easiest way to do this is to save the initial position before rotating and just restore it afterwards.

@andeeee:

Can you tell me how to restore the initial rotation? I know how to restore the initial position, but if the initial rotation is zero, that doesn’t help. I.E. I need to undo all of the rotations that have occurred.

Wouldn’t you just store it a variable??? I mean like, maybe write something like “var initialRotation:Quaternion = transform.rotation;”? It shouldn’t be too hard, I think, unless I don’t understand what you’re saying.

@ liars_paradox:

If I instantiate the Game Object as below the GO will be aligned with the Y axis:

var InitialRot    : Quaternion = Quaternion.identity;
var GO : GameObject;

GO         = (Instantiate(Prefab, InitialPos, InitialRot));

Then I will rotate the GO about the z axis multiple times, so that the GO is no longer aligned with the Y axis.

If I rotate GO about the z axis multiple times, do I need to store the cummulative rotations to figure out how much rotation to undo? Because if I use InitialRot, since it is no rotation, it won’t get the GO to align with the Y axis again.

Don’t undo any Rotations.

Quaternion initialRot = Quaternion.Identity;

void Start()
{
initialRot = transform.rotation;
}

void revert()
{
transform.rotation = initialRot;
}

This sets up initialRot by default to along the Y axis, but it will also save the rotation on Object instantiation if you want to instantiate it to a different orientation than on the y. This allows you to revert to original. And if you want to save a new Orientation to Revert, just set initialRot again.

Edit: In your context:

var InitialRot : Quaternion = Quaternion.identity;
var GO : GameObject;

GO = (Instantiate(Prefab, anyPosition, anyRotation));

initialRot = GO.transform.rotation;

Revert()
{
transform.rotation = intialRot;
}

@ Ntero:

Sweet, that worked. Thanks!