Camera orbit around object without RotateAround

Hi, I want my camera to orbit around an object.
Using RotateAround is fine for orbiting left and right like so:

transform.RotateAround (selected.transform.position, new Vector3 (0,1,0),Input.GetAxis("Mouse X")* 20 * Time.deltaTime);

But for going bottom to top it doesn’t work right eg:

transform.RotateAround (selected.transform.position, new Vector3 (1,0,0),Input.GetAxis("Mouse Y")* 20 * Time.deltaTime);

or:

transform.RotateAround (selected.transform.position, new Vector3 (0,0,1),Input.GetAxis("Mouse Y")* 20 * Time.deltaTime);

I even tried mixing and inverting vector values eg:

transform.RotateAround (selected.transform.position, new Vector3 (1,0,1),Input.GetAxis("Mouse Y")* 20 * Time.deltaTime);
transform.RotateAround (selected.transform.position, new Vector3 (1,0,-1),Input.GetAxis("Mouse Y")* 20 * Time.deltaTime);

But the ratation axiz doesn’t seem to work, is there another way to achieve this? PS: I also want the camera to stop at top or bottom positions.

Basically I want the camera to act like in Homeworld 2

The axis that you will be performing the up/down movement changes as you orbit the camera around an object. Cross products are your friend here.

This solution assumes that your camera is parented to the object you want to rotate around, in this case, the player.

//Get rotation amount/input
float rotationAngle = x;
Vector3 direction = new Vector3(transform.localPosition.x,0,transform.localposition.z);
//Gonna need a direction, not a rotation
Vector3 pitchAxis = Vector3.Cross(direction,Vector3.up)
//Compare it to (0,1,0) and get a vector perpendicular to the two
transform.RotateAround(transform.parent.position,pitchAxis,rotationAngle)

I know this topic is 5 years old, but when I was doing some searches, it was the first thing that came up, so I think it’s worth having a solution here.

I’ll also note that using RotateAround might not be the best way to do things, in my case, certain inputs cause a bit of spazzing out, which is frustrating to say the least.

Another thing to look out for when using RotateAround is the gimbal lock. As you change the yaw and pitch, your camera may roll a bit. Quaternion.Euler will let you set your roll to zero without consequence.

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

Hope this helps somebody!