0
\$\begingroup\$

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

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can't you just set the rotation to your fixed boundary if you go over it? \$\endgroup\$
    – Zibelas
    Jun 6 at 13:05
  • \$\begingroup\$ Sorry, I have very little knowledge on unity, can you please give me any idea about how can I do it. Thank you \$\endgroup\$
    – Zishan
    Jun 6 at 13:15
  • \$\begingroup\$ Have you checked previous questions about limiting (also known as clamping) rotation angles in Unity? \$\endgroup\$
    – Pikalek
    Jun 6 at 15:15

0

You must log in to answer this question.

Browse other questions tagged .