Combining two camera modes

I’ve been experimenting with a vehicle-based game where you can drive and fire a turret. I’d like the camera to follow the vehicle while turning, but also have it orbit around the vehicle to aim the turret at the same time.

These two (simplified) scripts work fine independently but trying to combine them is giving me an headache.

My follow script:

float currentAngle = transform.eulerAngles.y;
float desiredAngle = target.transform.eulerAngles.y;
float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime);
         
var rotation = Quaternion.Euler(0, angle, 0);
transform.position = target.transform.position - (rotation * offset);
         
transform.LookAt(target.transform);

My mouse orbit script:

x += Input.GetAxis("Mouse X") * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * Time.deltaTime;

var rotation = Quaternion.Euler(y, x, 0f);

var position = rotation * target.position;
transform.rotation = rotation;
transform.position = position + new Vector3(0f, 2.4f, 0f);

Here’s a script I have used previously, maybe it would be useful to you!

Note that the code currently in FixedUpdate was just for testing purposes to see if this was the correct code, it is not advisable to set the velocity of a rigidbody directly and there are better choices for controlling rotation than is used here as well!

public GameObject target;
Rigidbody targetRB;
Transform targetTrans;
Transform thisTrans;
public Vector3 orbitOffset = new Vector3(0, 2, 0);
float viewAngle = 0;
float rot = 0;
public float rotationSpeed = 360;
public float zoomSpeed = 10;
float distance;
public float minDist = 5;
public float maxDist = 10;
public float maxSpeed = 10;
Vector3 lookPos;
Quaternion origRot;

void Start(){
	targetRB = target.rigidbody;
	targetTrans = target.transform;
	distance = Mathf.Clamp(distance, minDist, maxDist);
	thisTrans = transform;
	lookPos = targetTrans.position + orbitOffset;
	origRot = thisTrans.rotation;
}

void Update(){
	lookPos = targetTrans.position + orbitOffset;
	if(targetRB != null){
		distance = Mathf.Lerp(minDist, maxDist, targetRB.velocity.sqrMagnitude/(maxSpeed*maxSpeed));
		distance = Mathf.Clamp(distance, minDist, maxDist);
	}
	
	viewAngle += Input.GetAxis("Mouse Y")*rotationSpeed*Time.deltaTime;
	rot += Input.GetAxis("Mouse X")*rotationSpeed*Time.deltaTime;
	viewAngle = Mathf.Clamp(viewAngle, -80, 10);
	
	Quaternion xQuaternion = Quaternion.AngleAxis (rot, Vector3.up);
	Quaternion yQuaternion = Quaternion.AngleAxis (viewAngle, Vector3.left);
	thisTrans.rotation = origRot * xQuaternion * yQuaternion;
	thisTrans.position = lookPos - (thisTrans.forward*distance*Mathf.Lerp(0.5f, 1, (10-viewAngle)/90));
}

void FixedUpdate(){
	//REPLACE THIS WITH BETTER MOVEMENT CODE
	if(targetRB == null){
		return;
	}
	Vector3 v = targetRB.velocity;
	v = Input.GetAxis("Vertical")*maxSpeed*targetTrans.forward;
	targetRB.velocity = v;
	targetTrans.Rotate(Vector3.up, Input.GetAxis("Horizontal")*rotationSpeed/2*Time.deltaTime);
}

Scribe