# Stopping transform.Rotate on an exact rotation value

I’m trying to write a script where if the player pushes the “A” key, the camera will rotate -90 degrees on the y-axis, thus moving the camera’s y-rotation from 0 to 270. The camera is meant to continue rotating even after the effects of Input.GetKeyDown have taken place, and should only stop when the y-rotation of the camera is 270 degrees. However, whenever I try to get the camera to stop rotating exactly when it’s at 270 degrees (ex. “if (transform.localEulerAngles.y == 270)”), the camera will almost always keep on rotating. The best fix I could come up with is changing “==” to “<=”, so that the transform.rotation function will stop if the rotation of the camera is less than or equal to 270, but this solution almost never stops right at 270 either. It’ll stop the transform.Rotate function a bit later (usually between 268 degrees and 269 degrees, depending on the rotation speed), so I set up another line of code that would set the rotation of the camera to 270 as soon as the transform.Rotate function stops. However, this leads to an annoying camera jitter that occurs when the rotation value of the camera suddenly changes between whatever rotation value the transform.Rotate function stopped on and 270 degrees, and if the player keeps pressing the “A” key the jitter will keep on occurring. Is there any way to have the transform.Rotate function end on an exact rotation value, or at least a way to remove the “jitter”?
Here is my code:

``````public GameObject cam;
public float rotateSpeed;
public bool isRotating;
public Transform targetRotation270;

// Use this for initialization
void Start () {
isRotating = false;
}

// Update is called once per frame
void FixedUpdate () {
if (Input.GetKeyDown(KeyCode.A))
{
isRotating = true;
RotateCam(270);
}
else if (!Input.GetKeyDown(KeyCode.A) && isRotating)
{
RotateCam(270);
}
}

void RotateCam (float targetAngle)
{
if (cam.transform.localEulerAngles.y >= targetAngle || cam.transform.localEulerAngles.y == 0)
{
cam.transform.Rotate(0, -rotateSpeed * Time.deltaTime, 0);
}
else
{
isRotating = false;
cam.transform.Rotate(0, 0, 0);
cam.transform.rotation = targetRotation270.rotation;
}
}
}
``````

Note: GameObject cam is the camera I’m trying to rotate, and Transform targetRotation270 has the rotation values (0, 270, 0).

Try using mathf.clamp:

Vector 3 LimitRot = cam.transform.localeulerangles;

LimitRot.y = Mathf.Clamp(LimitRot,0,270);

cam.transform.localeulerangles = LimitRot;

That should work but note it is untested!