How to clamp camera rotation in mobile unity?

Share on social media

In this article, I will show you how you can clamp camera rotation in unity on Android or iOS Mobile.

I will show you how you can create a simple script of the camera clamping in unity that will work on any platform like PC, Mobile devices.

clamp camera rotation in android unity
clamp camera rotation in android unity

Before starting, There are many assets available on the unity assets store, here is the asset to clamp cameras in mobile devices such as orbit cam. Let’s Create a script to clamp camera in mobile devices in unity.

Clamp camera rotation C# script

using UnityEngine;

public class CameraRotaionWithClamping: MonoBehaviour
{

    [Header("Min Max Value Of Vertical Rotation")]
    public bool isClampVerticalRotation;
    public float verticalMinValue;
    public float verticalMaxValue;

    [Space()]
    [Header("Min Max Value Of Horizontal Rotation")]
    public bool isClampHorizontalRotation;
    public float horizontalMinValue;
    public float horizontalMaxValue;

    private Vector3 firstpoint; 
    private Vector3 secondpoint;

    private float xAngle; 
    private float yAngle;
    private float xAngTemp;
    private float yAngTemp;

    // Booleans
    private bool isUpdatePosition = false;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            OnStartTouch();
            isUpdatePosition = true;
        }

        if(Input.GetMouseButtonUp(0))
        {
            isUpdatePosition = false;
        }
        
        if(isUpdatePosition)
        {
            UpdatePosition();
        }
    }

    private void OnStartTouch()
    {
        firstpoint = Input.mousePosition;

        xAngTemp = xAngle;
        yAngTemp = yAngle;
    }

    public void UpdatePosition()
    {
        secondpoint = Input.mousePosition;

        xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
        yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;

        // Clamping Vertical Value
        if (isClampVerticalRotation)
        {
            yAngle = Mathf.Clamp(yAngle, verticalMinValue, verticalMaxValue);
        }

        // Clamping Horizontal Value
        if (isClampHorizontalRotation)
        {
            xAngle = Mathf.Clamp(xAngle, horizontalMinValue, horizontalMaxValue);
        }

        transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
    }
}

Attach this script to the camera first, Then in order to clamp camera rotation set the camera clamping values in the unity editor

You can decide in which directions you want to clamp your camera whether horizontally or vertically.

In any case, don’t forget to check the bool of direction in which you want to clamp your camera on your android mobile.

Here is a sample of my script

How to clamp camera rotation in unity
Clamp camera rotation

So here as you can see, I wanted my camera to be clamped in both directions X and Y.

So first I checked both of the booleans to clamp the camera in both directions then I set the Min and Max values of the camera rotation.

In case you don’t want to clamp the camera in both directions you can check the boolean of the particular direction In which you want your camera to clamp and check the boolean False to rotate the camera without clamping.

I hope guys this article was helpful for you, Comment your thoughts or if you have any questions, issues, or any query please feel free to comment, Thank you.

How do you clamp the camera rotation in unity? Let me know in the comment section.

Aakash Solanki
Aakash Solanki

I am a professional unity game developer. I have a passion for creating immersive gaming experiences. For me writing articles feels like giving back to the community we have learned from. My hobbies involve playing games and writing articles and sometimes I also create youtube videos.


Share on social media

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top