Home > OS >  How to set Unity3D vertical camera rotation limit
How to set Unity3D vertical camera rotation limit

Time:06-07

I want to rotate my camera horizontally around the object 360 degree and want to limit my vertical rotation from -45 to 45 degrees. I have found some solution over internet but none of them are work for mouse click input. Here is my working code without vertical rotation limit.

[SerializeField]
private Camera _camera;

[SerializeField]
private Transform target;

private Vector3 previousPosition;



// Start is called before the first frame update
void Start()
{
    
}

Update is called once per frame
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        previousPosition = _camera.ScreenToViewportPoint(Input.mousePosition);
    }

    if (Input.GetMouseButton(0))
    {
        Vector3 direction = previousPosition - _camera.ScreenToViewportPoint(Input.mousePosition);

        _camera.transform.position = target.position;

        _camera.transform.Rotate(new Vector3(1, 0, 0), direction.y * 180);
        _camera.transform.Rotate(new Vector3(0, 1, 0), -direction.x * 180, Space.World);
        _camera.transform.Translate(new Vector3(0, 0.3f, -3));

        previousPosition = _camera.ScreenToViewportPoint(Input.mousePosition);


    }

}

If someone can point me how can I limit the vertical rotation that would be very helpful for me.

Thank you

CodePudding user response:

they way u codded that camera rotation system is great, but it might have some problems , i highly recommend following a guy called Brakeys on youtube that made a perfect moving and rotating systems . anyway u can use a method called Mathf.Clamp to set the minimal and maximum numbers of a float without exceeding those limits . this is an example that isn't going to work but rather demonstration. float cameraLimits = Mathf.Clamp(MouseY, 20 ,90);

CodePudding user response:

Let's be honest, the Rotate command is a relative method and works to limit inefficiencies. To solve the problem of limiting the camera, you must have fixed and controllable variables. Attach the code below to the camera, which solves the constraint problem, but keep in mind that the camera is limited in its local angles. To determine the angle and direction of the camera, child empty it and rotate empty in the main direction put,

public float LimitAngleX = 45;
public float LimitAngleY = 180;

private float AngleX;
private float AngleY;

public float sensitivity = 10f;
public void Update()
{
    var angles = transform.localEulerAngles;

    var xAxis = Input.GetAxisRaw("Mouse X") * Time.unscaledDeltaTime * sensitivity;
    var yAxis = Input.GetAxisRaw("Mouse Y") * Time.unscaledDeltaTime * sensitivity;

    AngleX = Mathf.Clamp (AngleX-yAxis, -LimitAngleX, LimitAngleX);
    AngleY = Mathf.Clamp (AngleY xAxis, -LimitAngleY, LimitAngleY);

    angles.x = AngleX;
    angles.y = AngleY;

    var euler = Quaternion.Euler (angles);
    
    transform.localRotation = euler;

    transform.localEulerAngles = angles;
}
  • Related