Clamp camera orbit rotation using RotateAround

I am actually working on a game where the player will be able to control an airship with keyboard and camera, but not like in FPS where we control a human, where a big mouse drag can lead to an immediate 360° movement. I want the player to follow slowly the camera direction, depending of the vehicle. The best example I can give for this type of movement is vehicle movements in Borderlands 2.

//Code was here, see updated version below

EDIT : I managed to clamp the height rotation using this post, adapting it with my needs. It now do what I want for height rotation, but it is messing with the side rotation… For unknown reason, I can’t go past 90° left or right. I didn’t modify the transform.RotateAround for the side rotation, so it’s something else in this that is causing the problem, but I can’t figure it out.

public Transform player;
	float heightRotation;
	float sideRotation;
	public float sensitivity;
	Vector3 initialVector;

	void Start () {
		sensitivity = 3;
		initialVector = transform.position - player.position;
		initialVector.x = 0;
	}
	
	// Update is called once per frame
	void Update () {

		//getting mouse inputs
		sideRotation = Input.GetAxis ("Mouse X");
		heightRotation = Input.GetAxis ("Mouse Y");


		//applying method seen on the post linked above, though I don't understand what ".x > 0 ? 1 : -1" is doing..
		Vector3 currentVector = transform.position - player.position;
		currentVector.x = 0;
		float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1);
		float upAngle = Mathf.Clamp(angleBetween + (heightRotation * sensitivity * Time.deltaTime), -75, 75);
		upAngle = upAngle - angleBetween;
		float sideAngle = sideRotation * sensitivity * Time.deltaTime;

		//applying calculated angles to the camera
		transform.RotateAround(player.position, Vector3.right, upAngle);
		transform.RotateAround(player.position, Vector3.up, sideAngle);
		transform.LookAt(player);
	}

Any clues about what is causing the side rotation unwanted clamping ? Also, it isn’t clamping roughly like it does for the height rotation. As the height angle values are capped, if I try to go above it, nothing happens, the camera isn’t moving at all. For the side rotation, it’s like two forces opposing together, if I move my mouse rapidly, it will go above 90°, but go back when I stop moving my mouse. And last thing, no matter the angle I chose for the clamp, the side rotation will stop at 90°, tried different values from 15 to 120°, working perfectly for height rotation… Thanks in advance !

This might have a couple of bugs in it (the camera might “pop” in a couple of locations), but I think it should get you close.

using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour
{
    public Transform player;
    public float sensitivityX = 5;
    public float sensitivityY = 5;

    void Start()
    {
        transform.LookAt(player.position);
    }

    void Update()
    {
        var sideRotation = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
        var heightRotation = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;

        var distance = (transform.position - player.position).magnitude;

        transform.position = player.position;
        var oldHeightRotation = transform.rotation.eulerAngles.x;

        transform.Rotate(-oldHeightRotation, 0, 0);
        transform.Rotate(0, sideRotation, 0);

        var max = 50f;
        var min = 320f;
      
        if (heightRotation > 0 && oldHeightRotation <= max + 1)
        {
            heightRotation = Mathf.Min(oldHeightRotation + heightRotation, max);
        }
        else if (heightRotation < 0 && oldHeightRotation >= min - 1) 
        {
            heightRotation = Mathf.Max(oldHeightRotation + heightRotation, min);
        }
        else
        {
            heightRotation = oldHeightRotation + heightRotation;
        }

        transform.Rotate(heightRotation, 0, 0);

        transform.Translate(transform.InverseTransformVector(transform.forward) * -distance, Space.Self);
    }
}

Here’s an alternative approach that is fairly simple and works as long as you have sensible bounds. I’m using it for an free-look RTS-style camera. This isn’t suitable for a free-space camera but maybe it will help others.

// worldPosition is the location of the object you're rotating around
private void UpdateRotationAndTilt(bool tiltButtonStatus, Vector3 worldPosition)
    {
        // Rotate and tilt
        if (!isTilting && tiltButtonStatus)
        {
            isTilting = true;
        }
        else if (isTilting && !tiltButtonStatus)
        {
            isTilting = false;
        }

        if (isTilting)
        {
            float currentTiltAngle = playerCamera.transform.eulerAngles.x;
            float sideRotation = Input.GetAxis("Mouse X") * rotateSensitivity * Time.deltaTime;
            float tiltRotation = Input.GetAxis("Mouse Y") * tiltSensitivity * Time.deltaTime;

            // When the mouse moves on the Y axis, tilt the camera in an arc around the point on the ground
            if ((tiltRotation > 0 && currentTiltAngle < tiltMaxAngle) || 
                (tiltRotation < 0 && currentTiltAngle > tiltMinAngle))
            {
                float angle = Mathf.Clamp(tiltRotation, tiltMinAngle - currentTiltAngle, tiltMaxAngle - currentTiltAngle);

                playerCamera.transform.RotateAround(worldPosition, playerCamera.transform.right, angle);
            }

            // When the mouse moves on the X axis, rotate the camera in an arc around the point on the ground
            if (sideRotation != 0)
            {
                playerCamera.transform.RotateAround(worldPosition, Vector3.up, sideRotation);
            }
        }
    }