- Home /
Setting an objects angle about a local axis without localEulerAngles?
Hey guys (and gals),
I'm looking to set my objects rotation around its local z axis to a specific value (in the below case 0). The problem is that I can't work out how to do it. I know people use Mathf.Atan2 for "look at" so I thought about using it for this but can't get it working. In the below code I am attempting to set "myObject"s rotation around its local z axis to 0.
Vector3 right = myObject.transform.right;
float angle = (Mathf.Atan2(right.z, right.x) * Mathf.Rad2Deg) + 180;
myObject.transform.RotateAround(myObject.transform.position, myObject.transform.forward, -angle);
So I'm trying to get its rotation around its local z axis in the second line and the adjust its rotation in the third line. The problem is that the second line just flat out doesn't work, the values it returns aren't the current rotation around the z axis and I'm not sure why.
I don't want to use eulerAngles because my objects rotation varies quite a lot meaning that sometimes rotating around the z axis is done with localEulerAngles.y and sometimes localEulerAngles.z so adjusting either one of them to 0 isn't necessarily beneficial :)
Thanks in advance,
I don't understand your question well enough to give you an answer, but you are passing the parameters to Atan2() in the wrong order. It is Atan2(y,x) or Atan2(z,x) for almost all situations in Unity. You are doing Atan2(x,z).
Yeah, sorry. You're right. It was one of the things I was trying to get it working and I forgot to revert it. Thanks! (i've tried both ATan2(y,x) and ATan2(z,x) :))
As for what I'm looking for, I'm basically looking for a way to store/set the rotation about an axis without using localEulerAngles. The reason being that I want to set one of my objects rotations to the same as another objects rotation whilst preserving its local z rotation.
I figured that the easiest way of doing that would be to: 1) Store object 1s local z rotation 2) Set object 1s rotation to the same as object 2 3) Set object 1s local z rotation to its original local z rotation
I don't think what you are attempting to do will work. If you are controlling the rotation directly through the Transform, then one solution is to use an empty game object with the visible game object as a child. Z rotations (and only Z rotation) would be done on the child, other rotations done on the parent. That way when you assigned the rotation of one object to the empty game object, the local 'z' rotation would not be effected.
The problem is that in order to generate that parent I'd have to set its rotation to (childRotation.x, childRotation.y, 0) and the child would then maintain its own z rotation. Setting the parents rotation in this way presents us with the same problem as before of adjusting an objects rotation (in this case the parents) to 0 around the local z axis.
Also assu$$anonymous$$g we do parent it correctly, if we do parent.eulerAngles = object2.eulerAngles then the child/object1 wouldn't have maintained its local z rotation, but rather it's local z rotation + object 2s local z rotation. Unless we can assign the parents individual axis rotations directly (I.e assign x and y, not z)?
Thanks for the help anyway :)