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);
↧