Issue with copying and applying rotation values

I am copying rotation values of my model into a Vector3 and then I re-apply it back to my model, the rotation values changes completely.

For example, my Model has rotation as (-180, 0, 0). I then copy it to a Vector3, the model’s rotation does not get copied. It is shown as (0, 0, 0). After that I finally add back the rotation to my model and the rotation of my model changes to (0, 180, 0). What is wrong with it?

public GameObject myModel;
public Vector3 rotationValues_;

public void Start(){
   rotationValues_ = myModel.transform.eulerAngles;
}

public void ResetValue(){
   myModel.transform.eulerAngles = rotationValues_;
}

Changing from -180 to 180 is normal. Look up something like “unity changes rotation values” and you should get plenty of explanations. In brief, Unity stores rotations in a standard form. Sometimes the numbers it uses look completely different than yours (especially for x greater than 180), but they are correct. Rotations are more complicated than most people think.

As for 0,-180,0 changing to 0,0,0 – that’s not right. You’re probably looking at it the wrong way, or too soon… .

1 Like

You’re providing rotations as euler angles. Rotations are stored in the Transform component as quaternions. What is happening here is you are converting euler angles to a quaternion, then converting it back to euler angles. That second conversion has no idea what the originally provided euler angles were. It will just be converted to a valid euler angles for that quaternion. -180 and 180 are the same rotation, so unless you have Unity’s quaternion to euler angles code handy to walk through, you can’t predict which one it will be.

This part sounds like you are confusing local space and world space. In the Inspector, you’re shown the local space rotation for the GameObject. Your code though is only accessing the world space rotation. If this is a child object with a different rotation than the parent, the world space and local space rotations will not be equal, so the value of myModel.transform.eulerAngles will not be the same rotation as you see in the Inspector.

1 Like

Ah, yes. That’s probably the 0,0,0 problem. If an object has a parent, Unity’s Inspector LIES about the rotation. It shows you the rotation from the parent. It never shows the true, final rotation. In code it’s easier: you can check transform.rotation (total, real facing) or transform.localRotation (the extra rotation after it copies the parent rotation).

To see this, take two objects with no rotation. Put one inside the other then rotate the parent. The child will rotate with it, but it’s displayed rotation won’t change. If you think of (0,0,0) as meaning “I point how my parent points, with no changes” that should make perfect sense. Try changing the child to face right from the parent (0,90,0). As you rotate the parent the child will spin with, always facing left of the parent, always keeping local rotation 0,90,0. It’s a similar idea of walking with someone and saying “I follow Beth 2 steps behind and 1 to the left”.

The way Unity does these things eventually makes perfect sense and is clearly the best way, and also the way everyone else does it. But if the Inspector changed from saying “Transform” to “LocalTransform” for children, it would be simpler for beginners.

1 Like