Looking for assistance with my camera.

Hello, I have been staring at this for quite some time. I have a fairly solid camera system here that follows units around and allows the player to pan, orbit, and zoom the camera. The issue I have with it is when the player does a compound orbit up/down and right/left at the same time, the camera will roll. I do not want the camera to roll but I haven’t been able to stop it.

Anyway here is the code, just put it on a camera and have an object in the scene, when you click on the object it will set it as the target.

Set the minDistance and maxDistance in the editor.

Just looking for a fix for the camera rolling.

using UnityEngine;
using System.Collections;

public class SpaceCamera : MonoBehaviour {
	
	// Class Variables
	public GameObject currentCameraTarget,oldCameraTarget,shipCamera;
	public float currentDistance, maxDistance, minDistance, setDistance, mouseChangeX, mouseChangeY;
	private Ray ray;
	private RaycastHit hit;
	private bool _shouldMoveCamera;
	private Vector3 newCameraPosition, oldCameraPosition, differenceVector, forwardAxis, upAxis, rightAxis;
	
	// Use this for initialization
	void Start(){
	}
	
	void GetTarget(){
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if(Physics.Raycast(ray,out hit,Camera.main.farClipPlane)){
			Debug.DrawLine(ray.origin, hit.point);
			if(Input.GetButtonDown("Fire1")){
				oldCameraTarget = currentCameraTarget;
				currentCameraTarget = hit.collider.gameObject;
				GlobalControlVariables.currentlySelected = hit.transform.gameObject;
				_shouldMoveCamera = true;
			}
		}
	}
	
	void CameraControl(){
		if(currentCameraTarget){
			
			// Look at the currently selected unit. (Translation)
			if(_shouldMoveCamera  oldCameraTarget){
				oldCameraPosition = this.transform.position;
				differenceVector.x = currentCameraTarget.transform.position.x - oldCameraTarget.transform.position.x;
				differenceVector.y = currentCameraTarget.transform.position.y - oldCameraTarget.transform.position.y;
				differenceVector.z = currentCameraTarget.transform.position.z - oldCameraTarget.transform.position.z;
				newCameraPosition.x = this.transform.position.x + differenceVector.x;
				newCameraPosition.y = this.transform.position.y + differenceVector.y;
				newCameraPosition.z = this.transform.position.z + differenceVector.z;
				
				_shouldMoveCamera = false;
				this.transform.position = newCameraPosition;
			}
			
			// Use the scroll-wheel to zoom in/out
			if(Vector3.Distance(this.transform.position,currentCameraTarget.transform.position) < maxDistance || Vector3.Distance(this.transform.position,currentCameraTarget.transform.position) > minDistance){
				transform.Translate(Vector3.forward * Input.GetAxis("Mouse ScrollWheel"));
			}
			currentDistance = Vector3.Distance(this.transform.position,currentCameraTarget.transform.position);
			if(Vector3.Distance(this.transform.position,currentCameraTarget.transform.position) > maxDistance){
				transform.Translate(Vector3.forward * 0.1f);	
			}
			if(Vector3.Distance(this.transform.position,currentCameraTarget.transform.position) < minDistance){
				transform.Translate(Vector3.forward * -0.1f);	
			}
			
			
			// On right-click pan, Orbit the camera around the target.
			if(Input.GetButton("Fire2")){
				mouseChangeX = Input.GetAxis("Mouse X");
				mouseChangeY = Input.GetAxis("Mouse Y");
				
				forwardAxis = Vector3.Normalize(currentCameraTarget.transform.position - transform.position);
				upAxis = transform.up;
				rightAxis = transform.right;
				
				Vector3.OrthoNormalize(ref forwardAxis,ref upAxis,ref rightAxis);
				
				transform.RotateAround(currentCameraTarget.transform.position, upAxis, mouseChangeX);
				transform.RotateAround(currentCameraTarget.transform.position, rightAxis, -mouseChangeY);
				transform.RotateAround(currentCameraTarget.transform.position, forwardAxis, Input.GetAxis("Mouse ScrollWheel"));
			}
			
			// On middle-click pan, pan the camera around in space
			if(Input.GetButton("Fire3")){
				mouseChangeX = Input.GetAxis("Mouse X");
				mouseChangeY = Input.GetAxis("Mouse Y");
				
				transform.Translate(-Vector3.right * mouseChangeX);
				transform.Translate(-Vector3.up    * mouseChangeY);
			}
		}
		
		else{
			
		}
	}
	
	// Update is called once per frame
	void LateUpdate () {
		GetTarget();
		CameraControl();
	}
}

If by rolling you mean rotating around the z-axis. Have you tried freezing the z-axis so that it can only rotate up/down and left/right?

Physics > RigidBody
Then at the bottom, Freeze Rotation > Z

Camera’s do not have rigidbodies, by default. The camera is free from the rotation and movements of the unit it is looking at.

Right, but you can add one to it.

Right, but adding the a rigidbody to the camera alone will not lock the camera’s roll because that does not stop the codes modification.