Orbit camera around player on both X and Y

I have the following code, which allows me to orbit the camera around the player, on the X axis.

float smoothFactor = 0.5f;
float rotationSpeed = 16.0f;
Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotationSpeed, Vector3.up);
_cameraOffset = camTurnAngle * _cameraOffset;
Vector3 newPos = transform.position + _cameraOffset;
Camera.main.transform.position = Vector3.Slerp(Camera.main.transform.position, newPos, smoothFactor);
Camera.main.transform.LookAt(transform.position);

I want to allow also for orbiting on the Y axis. I can’t seem to get it to combine both the X and Y, does anyone have any advice?

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager. Plenty of tutorials out there for typical game camera setups / usage patterns.

2 Likes

Yeah, I’m finding it quite confusing! I have 2 camera ‘modes’ now: “flying” and “walking”.

I’ve also been trying to smoothly transition from one to the other (they both use the same camera). Do you know if that’s possible with Cinemachine? I’m really happy with my “flying” camera, is it possible to transition from one camera to another (Cinemachine)?

Oh yes. It does pretty much anything you can imagine, or there are example scripts to tack on to finish the job.

1 Like

Thank you Kurt, I’ve switched to Cinemachine and have even ended up with a better flying cam than the one I programmed myself.

2 Likes

You’re welcome!

So far all the answers I have seen here when I suggest using Cinemachine are:

  1. “It’s awesome, thanks!”

  2. “I refuse to even try it.”

Happy to see you are in group 1!

1 Like