# Help me write a camera script, please.

I have this script so far:

public Transform target;
public float Speed = 5;
public float Sensitivity = 1;
float currentX;
float currentY;
float y;
float x;

void Update()
{
transform.RotateAround(Vector3.zero, Vector3.up, Speed * Time.deltaTime);

currentX = target.transform.rotation.eulerAngles.x;
currentY = target.transform.rotation.eulerAngles.y;

if (Input.GetMouseButton(0))
{
y += Input.GetAxis(“Mouse X”) * Sensitivity;

x -= Input.GetAxis(“Mouse Y”) * Sensitivity;
x = Mathf.Clamp(x, 0f, 90f);

transform.rotation = Quaternion.Euler(x, y, 0);
}
}

The camera rotates around the car and what should happen when I press left click is that the camera stops rotating and you can rotate the camera manually. The problem is that when I click the camera goes back to the default rotation of 0 and then you can rotate it normally like you’re supposed to. I imagine I need to get the current rotation of the transform and somehow make the manual rotation to start from there, the thing is I have now idea how. I have tried adding a currentX/Y value to the x and y values in the quaternion, but normally the camera goes crazy, multiplying makes no sense at this point either. Quaternion.Lerp doesn’t work because you can’t convert directly a float to quaternion.

``````currentX = target.transform.rotation.eulerAngles.x;
currentY = target.transform.rotation.eulerAngles.y;

if (Input.GetMouseButton(0))
{
y = currentY + Input.GetAxis("Mouse X") * Sensitivity;
x = currentX + Input.GetAxis("Mouse Y") * Sensitivity;
transform.rotation = Quaternion.Euler(x, y, 0);
}
else {
transform.RotateAround(Vector3.zero, Vector3.up, Speed * Time.deltaTime);
}
``````