Third Person Camera View Switcher

Hello all.

I need a third person camera controller that is parented to the player and when the user presses a key, it switches between 4 orthogonal views: front, rear, left and right. I’ve got most of this simple code but two issues are pestering me:

The first is related to rotation angle wandering. When the player starts to move around and switch views, the normal angles of 90, 180, 270 and 360 do not resolve to their intented positions and hence, for instance, the front view may be somewhat skewed. The problem seems to be that the calculation is being made from a global point of view, instead of a local one, but haven’t been able to figure out.

The second one has to do with the parsing of the input key: it reads the same activation several time and hence it switches pretty fast from one view to the following ones as if the player had pressed the key multiple times.

public class CameraController : MonoBehaviour {

	public GameObject personalCamera;
	public GameObject target;

	public bool cameraRear = true;
	public bool cameraFront = false;
	public bool cameraLeft = false;
	public bool cameraRight = false;

	private Vector3 positionRear, positionFront, positionLeft, positionRight;
	private Vector3 rotationRear, rotationFront, rotationLeft, rotationRight;

	void Awake () {
		positionRear = new Vector3 (0, 1, -2);
		positionFront = new Vector3 (0, 1, 2);
		positionLeft = new Vector3 (-2, 1, 0);
		positionRight = new Vector3 (2, 1, 0);

	void LateUpdate () {
		if (Input.GetKey (KeyCode.Tab)) {

			if (cameraRear) {
				// Switch it to Front...
				cameraRear = false;
				Debug.Log("To Front");
				personalCamera.transform.position = target.transform.position + positionFront;
				personalCamera.transform.rotation = Quaternion.Euler(target.transform.rotation.x, target.transform.rotation.y + 180, target.transform.rotation.z);
				cameraFront = true;
			else if (cameraFront) {
				// Switch it to Left...
				cameraFront = false;
				Debug.Log("To Left");
				personalCamera.transform.position = target.transform.position + positionLeft;
				personalCamera.transform.rotation = Quaternion.Euler(target.transform.rotation.x,target.transform.rotation.y + 90, target.transform.rotation.z);
				cameraLeft = true;
			else if (cameraLeft) {
				// Swith it to Right...
				cameraLeft = false;
				Debug.Log("To Right");
				personalCamera.transform.position = target.transform.position + positionRight;
				personalCamera.transform.rotation = Quaternion.Euler(target.transform.rotation.x, target.transform.rotation.y + 270, target.transform.rotation.z);
				cameraRight = true;
			else if (cameraRight) {
				// Switch it to Rear...
				cameraRight = false;
				Debug.Log("To Rear");
				personalCamera.transform.position = target.transform.position + positionRear;
				personalCamera.transform.rotation = Quaternion.Euler(target.transform.rotation.x, target.transform.rotation.y + 360, target.transform.rotation.z);
				cameraRear = true;

Your help would be greatly apprecited. Thank you.

The second problem can be solved by using Input.GetKeyDown instead of Input.GetKey.

The first problem needs a better description. What do you mean by “somewhat skewed”?