Limit Y Axis transform.RotateAround


I have a camera that I want to rotate around a point (0,0,0) in all directions, but I want to put a clamp on it so that it can’t go too far above or below the point. I have seen this question answered for the left and right directions before but never for the vertical one.

I have tried converting the code from these two questions (that basically say the same thing) to work in the vertical direction, but it bugs out at some points along the rotation, and I can’t figure out why.

First Question, Second Question

And this is how I tried to convert it:

//how much we want to rotate by this frame
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed; //(before clamping)

//find current direction
Vector3 currentDirection = transform.position -;

//find current angle between basis for clamp & where we are now
float angle = Vector3.Angle(Vector3.forward, currentDirection);

 //finds out if it's up or down
if (Vector3.Cross(Vector3.forward, currentDirection).x < 0) angle = -angle; 

 //find out how much you can move without violating limits
 float newAngle = Mathf.Clamp(angle + rotY, yMinLimit, yMaxLimit);

//grabs how much you are allowed to move the angle from the current angle
rotY = newAngle - angle;

//spinning the garden
transform.RotateAround(, Vector3.up, rotX);
 transform.RotateAround(, transform.TransformDirection(Vector3.right), -rotY); //vertical rotation

If anyone knows of the correct way to make this work for the Y axis, or a different way to clamp the vertical rotation, I would be super excited to hear it! Ty!

Edit: Seems to be other interest in this question here: Clamp Camera Angle not Working - Questions & Answers - Unity Discussions

A very lovely person a StackOverflow gave me the answer to this question! For anyone finding this in the future here ya go: c# - How to Limit (clamp) Y axis Rotation for transform.rotatearound Unity - Stack Overflow

And here are the parts of it I used: (because he adds in some stuff about changing distances and stuff)

void Start()
        //vector from where I am to what I'm rotating around
        initialVector = transform.position -; 

        //current angles of camera (make sure it's looking at what you want
        //to rotate around)
        rotYAxis = transform.eulerAngles.y;
        rotXAxis = transform.eulerAngles.x;
        //distance between me and what I'm rotating around
        distance = Vector3.Magnitude(initialVector);

//You can probably call this in update I just call it from a dif script
public void Orbit()
       //get your inputs
        rotYAxis += Input.GetAxis("Mouse X") * thirdRotSpeed;
        rotXAxis -= Input.GetAxis("Mouse Y") * thirdRotSpeed;

       //clamp the angle
        rotXAxis = ClampAngle(rotXAxis, thirdMin, thirdMax);

       // convert it to quaternions
        Quaternion toRotation = Quaternion.Euler(rotXAxis, rotYAxis, 0);
        Quaternion rotation = toRotation;

       //figure out what your distance should be (so that it's rotating around 
       //not just rotating)
        Vector3 negDistance = new Vector3(0, 0, -distance);
        Vector3 position = rotation * negDistance +;

       //and apply!
        transform.rotation = rotation;
        transform.position = position;

//clamp angle from before
public static float ClampAngle(float angle, float min, float max)
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);

Thanks so much again to Fenixrw on StackOverflow for the help!