0
\$\begingroup\$

I've been trying to limit the Y-axis of the camera rotation but I'm out of depth until now. I'd looked up many tutorials and I found the clamping method, but I seem can't get it to work on my code, and all the tutorials I found is using them in Update(), while I'm using a button to rotate the camera. Here's the code:

public void RotateLeft()
{
    cameraY = Mathf.Clamp(cameraY, minAngle, maxAngle);
    if(cameraY >= minAngle)
    {
        Camera.main.transform.Rotate(0,-cameraY,0);
    }
}
public void RotateRight()
{
    cameraY = Mathf.Clamp(cameraY, minAngle, maxAngle);
    if(cameraY <= maxAngle)
    {
        Camera.main.transform.Rotate(0,cameraY,0);
    }
}

Does clamping works outside an Update()? What am I missing?

Thank you in advance.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I finally solved it. Thanks to this Unity Question though..

public void RotateLeft()
{
    myRotation.y = Mathf.Clamp(myRotation.y - 1f, -45f, 45f);
    Camera.main.transform.rotation = Quaternion.Euler(myRotation);
}
public void RotateRight()
{
    myRotation.y = Mathf.Clamp(myRotation.y + 1f, -45f, 45f);
    Camera.main.transform.rotation = Quaternion.Euler(myRotation);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .