- Home /
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).
Answer by anthot4 · May 04, 2018 at 07:51 AM
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!
Thank you for your help! However, now the camera rotates immediately to 270, no matter what rotateSpeed is set. Additionally, the same problem of transform.Rotate not stop precisely persists. I placed the code you provided in FixedUpdate() before the Input.Get$$anonymous$$eyDown functions. Any suggestions?
Sorry, I should have made it clearer where to put it, put the code in the rotatecam function after you have rotated your camera on line 28. I would remove the if statement you have in that function and after my code put something like this:
if (LimitRot == 270){ // if we have reached 270 degrees
isRotating = false;
}
Your answer
Follow this Question
Related Questions
Transform.RotateAround Issues 1 Answer
transform.rotate 4 Answers
Trouble rotating a character to correctly match new gravity direction 0 Answers
transform.Rotate slows down my moving cube 0 Answers
Clamping X rotation on camera. 1 Answer