- Home /
Rotation jumping to a different rotation after rotating a certain amount
Here is the result of the script (what the component looks like).......
I made a copy rotation script where it takes a desired object and copies its rotation to the object that the script has. It works, but then once I added an influence variable so that the object rotates based on the influence, things started acting weird. It works perfectly if the influence is set to 1, or 0 (1 meaning that it copies the rotation fully, and 0 meaning it doesn't copy the rotation at all), but if I set influence to something in between such as 0.5 the rotation of the object makes jumps in rotation every now and then. It seems like if I rotate the copyObject too far, the current object will make a jump in rotation, and then if I go too far again, then it will make another jump (and so on).
Here is the script...........
public class RotationConstraintCD : MonoBehaviour {
[SerializeField]
bool freezeX;
[SerializeField]
bool freezeY;
[SerializeField]
bool freezeZ;
[SerializeField]
[Range(0, 1)]
float influence;
float desiredX;
float desiredY;
float desiredZ;
float finalX;
float finalY;
float finalZ;
[SerializeField]
Transform copyTransform;
void Update()
{
if (!freezeX)
{
desiredX = copyTransform.eulerAngles.x;
finalX = (desiredX * influence);
}
if (!freezeY)
{
desiredY = copyTransform.eulerAngles.y;
finalY = (desiredY * influence);
}
if (!freezeZ)
{
desiredZ = copyTransform.eulerAngles.z;
finalZ = (desiredZ * influence);
}
transform.rotation = Quaternion.Euler (new Vector3(finalX, finalY, finalZ));
}
}