Problem with euler angles, randomly spinning around axis

Hi,

I’ve got a model,
that has a script attached to it,
three thing are going on.

transform.eulerAngles.z = 23;
transform.position.x = jointb.transform.position.x;
transform.position.y = jointb.transform.position.y;

Debug.Log (transform.localEulerAngles.z);

I am basically parenting the model manually to an other Game Object, that is defined in a function Start. The problem I have is that whenever I try to change the euler Angles or the local Euler Angles, the model starts spinning like crazy around the world y axis, no matter what number I write.

I’ve tried everything I could think of.
And instead of seting the angle value, it’s as if it was incrementing the value every cycle.

The reason I am using this instead of rotate, is because my goal is to set the .z angle using (Mathf.Atan2(jointp.transform.position.y, jointb.transform.position.x) *Mathf.Rad2Deg)

Before I imported this model, it worked fine with a placeholder.
Any help is welcome.

The code above contains a sin: it violates the commandment “Thou shall not modify a single eulerAngles axis”.

Despite the docs say eulerAngles is a Vector3 variable, it’s not: it’s actually a property - its getter routine converts the quaternion Transform.rotation to a 3-axes format and return it as a Vector3 structure. But 3-axes is a redundant way to define a rotation: there are several combinations that give the same result - (0, 180, 0) and (180, 0, 180) produce the same rotation, for instance. Thus, when converting from a quaternion (which is a single rotation around an arbitrary axis) to 3-axes, the routine must decide which combination to return - and many times it’s not the one we want.

When you write this:

transform.eulerAngles.z = 23;

the actual code generated is something like this (fictitious function names):

var temp: Vector3 = ConvertQuaternionToEuler(transform.rotation); 
temp.z = 23;
transform.rotation = ConvertEulerToQuaternion(temp);

The problem is in the first line: the values returned by the function may not be what you expect. As in the example, it could return (180, 0, 180) or (0, 180, 0) for a given rotation - and modifying z alone produces different results: (180, 0, 23) is not the same thing as (0, 180, 23) - one is upside down relative to the other, to be more precise.

To avoid this problem, you should use:

transform.eulerAngles = Vector3(0, 0, 23);

The x and y values in this case are 0, but they may be any valid angle - except eulerAngles.x and eulerAngles.y, of course!.

NOTE: The eulerAngles.z thing is wrong, for sure, but doesn’t seem to be enough to make the object spin continuously; maybe something else in your code is adding to the eulerAngles problem to produce this effect.