rotation goes crazy when setting eulerAngle

I am going like this in code

arm.localEulerAngles.x =
Mathf.Atan2
(
armAim.localPosition.z - arm.localPosition.z,
armAim.localPosition.y - arn.localPosition.y
)

  • Mathf.Rad2Deg;

What this is, it’s using trig to make the arm aim at armAim locator on the x axis. This works, but it does something weird. If this sets arm.localEulerAngles.x to something less than 180, than it will start to vibrate back and forth 180 degrees, it’ll flip 180 degrees every frame.

I don’t understand why it’s doing this. I’ve tried commenting out everything else in code but no help. Does anyone have any ideas?

I fixed this problem with this code…

its really funky and hard to explain, I don’t fully get it myself

	//this is some narby ass shit that needs to be done because of the way unity adds euler angles onto a joint to rotate
	//if the X rotation is below 90, then the Z and Y have to flip to allow the joint to rotate that way
	//this stores the X rot angle in rightCollarXRot then tests if its above or below 90
	//then applies appropriate rotation to other angles
	var rightCollarXRot : float = Mathf.Atan2(IKhandles.rightCollarIK.localPosition.z - joints.rightCollar.localPosition.z,
	IKhandles.rightCollarIK.localPosition.y - joints.rightCollar.localPosition.y	) * Mathf.Rad2Deg;	

	if (rightCollarXRot > 90)
	{
		joints.rightCollar.localEulerAngles.x = 180 - rightCollarXRot; 
		joints.rightCollar.localEulerAngles.y =  Mathf.Atan2(joints.rightCollar.localPosition.x - IKhandles.rightCollarIK.localPosition.x, 
		joints.rightCollar.localPosition.z - IKhandles.rightCollarIK.localPosition.z) * Mathf.Rad2Deg;
		joints.rightCollar.localEulerAngles.z = 180;
	}
	else if (rightCollarXRot < 90)
	{
		joints.rightCollar.localEulerAngles.x = rightCollarXRot; 
		joints.rightCollar.localEulerAngles.y =  Mathf.Atan2(joints.rightCollar.localPosition.x - IKhandles.rightCollarIK.localPosition.x, 
		joints.rightCollar.localPosition.z - IKhandles.rightCollarIK.localPosition.z) * Mathf.Rad2Deg + 180;
		joints.rightCollar.localEulerAngles.z = 0;
	}