# Getting the camera to follow target and rotate around target

Hey guys. I have been trying to figure out how to do this for around 45 minutes. I am trying to get the camera to follow a target while allowing for the use of the Unity Engine function rotatearound().

This is what i have got at the moment for moving the camera to rotate around:

``````if (Input.GetKey(KeyCode.A))
{
degrees += 60f * Time.deltaTime;
transform.RotateAround(target.position, Vector3.up, degrees);
}
``````

That does the rotation correctly. The problem i am having is when i use the below code to follow the target it will negate the rotatearound because i am setting the position to be changed. What can i use to let the camera do both of these things?

`````` Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
``````

Thanks

So i think i would need to answer my own question. I managed to solve it with clamping and zooming.

`````` public Transform cameraTarget;
private float x = 0.0f;
private float y = 0.0f;
private int HorizontalSpeed = 5;
private int VerticalSpeed = 2;

public float MaxViewDistance = 25f;
public float MinViewDistance = 10f;
public int ZoomRate = 20;

private float CamDistance = 3f;
private float DesiredDistance = 0f;
private float CorrectedDistance = 0f;

// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
}

void LateUpdate()
{

x += Input.GetAxis("Horizontal") * HorizontalSpeed;
y += Input.GetAxis("Vertical") * VerticalSpeed;

y = ClampAngle(y, 20, 80);
Quaternion rotation = Quaternion.Euler(y, x, 0);

DesiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ZoomRate * Mathf.Abs(DesiredDistance);
DesiredDistance = Mathf.Clamp(DesiredDistance, MinViewDistance, MaxViewDistance);
CorrectedDistance = DesiredDistance;

Vector3 position = cameraTarget.position - (rotation * Vector3.forward * DesiredDistance);

transform.rotation = rotation;
transform.position = position;
}

private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle += 360;
}

if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
``````