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.
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!