How to rotate camera around point relative to its current rotation?

How to rotate camera around point relative to its current rotation?

This is what I tried but it doesn’t take into consideration the cameras current rotation.

``````using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour
{
public Transform target;
private int distance = 100;

void Start()
{
transform.position = new Vector3(1, 1, 1);
transform.LookAt(target);
}

void Update()
{
var mousePosition = Input.mousePosition;
if (mousePosition.x < distance)
{
transform.RotateAround(Vector3.zero, Vector3.up, Mathf.Abs(mousePosition.x - distance) / 100);
}
if (mousePosition.x > Screen.width - distance)
{
transform.RotateAround(Vector3.zero, Vector3.down, (mousePosition.x - Screen.width + distance) / 100);
}
if (mousePosition.y < distance)
{
transform.RotateAround(Vector3.zero, Vector3.right, Mathf.Abs(mousePosition.y - distance) / 100);
}
if (mousePosition.y > Screen.height - distance)
{
transform.RotateAround(Vector3.zero, Vector3.left, (mousePosition.y - Screen.height + distance) / 100);
}
}
}
``````

If you need to rotate around a point you need to specify that pivot point in your RotateAround method. For example:

``````         Vector3 pivotPoint  = new Vector3(1.0f,2.0f,3.0f); //modify that value
if (mousePosition.x < distance)
{
transform.RotateAround(pivotPoint , Vector3.up, Mathf.Abs(mousePosition.x - distance) / 100);
}
``````

At the end of your Update method, you need to call LookAt method again if you dont want to loose target:

``````transform.LookAt(target);
``````