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