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\$
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);
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.