How to rotate and move the camera with correct orientation,How to move camera after it gets rotated

So I got stuck … I’m making a simulation game and I want to be able to both move and rotate the camera. My code works when I’m using only one of the above mentioned BUT when used together they have not the desired result. eg first rotate the camera 90 degrees and after that try to move the camera forward then it will move to the left.

Here’s my code :

	public Camera cam;
	public Transform camTransform;
	public GameObject centerPole;
	float secsHoldingRight = 0f;
	Vector3 lastMousePosition;
    public float rotationSensitivity = 1f;
    public float movingSensitivity = 10f;

    void Start() {
		camTransform = cam.gameObject.transform;
		centerPole = GameObject.Find ("centerOfScreen");
	}

	void Update () {

        MoveCamera();

        //Rotate camera
        if (Input.GetMouseButton (1)){
		secsHoldingRight += Time.deltaTime;

		    if (secsHoldingRight > 0.2f) {
			    if (Input.mousePosition.x > lastMousePosition.x) {
				    RotateCamera (rotationSensitivity * Time.deltaTime * (Input.mousePosition.x - lastMousePosition.x));
			    } 
			    else if (Input.mousePosition.x < lastMousePosition.x) {
				    RotateCamera (rotationSensitivity * Time.deltaTime * (Input.mousePosition.x - lastMousePosition.x));
			    }
		    }
		}
		else{
			secsHoldingRight = 0f;
		}
		lastMousePosition = Input.mousePosition;
	}

	void RotateCamera(float rotationAmount){
		cam.transform.RotateAround (Vector3.zero, Vector3.up, rotationAmount * 10f);
	}

    void MoveCamera()  {
        if (Input.mousePosition.y >= Screen.height * 0.95) {
            cam.transform.position += Vector3.forward * (Time.deltaTime * movingSensitivity);
        }
        else if (Input.mousePosition.y <= Screen.height * 0.05) {
            cam.transform.position += Vector3.back * (Time.deltaTime * movingSensitivity);
        }

        if (Input.mousePosition.x >= Screen.width * 0.95) {
            cam.transform.position += Vector3.right * (Time.deltaTime * movingSensitivity);
        }
        else if (Input.mousePosition.x <= Screen.width * 0.05) {
           cam.transform.position += Vector3.left * (Time.deltaTime * movingSensitivity);
        }
    }
}

You are moving your camera along the global xyz axis, if you move it along your cameras relative axises it will work.


For example; Vector3.left is always (-1, 0, 0), no matter what your cam rotation looks like and where you call it.

cam.transform.left will return a vector pointing to the left seen from your cameras relative space.


Try this:

void MoveCamera()  {
     if (Input.mousePosition.y >= Screen.height * 0.95) {
         cam.transform.position += cam.transform.forward * (Time.deltaTime * movingSensitivity);
     }
     else if (Input.mousePosition.y <= Screen.height * 0.05) {
         cam.transform.position += cam.transform.back * (Time.deltaTime * movingSensitivity);
     }

     if (Input.mousePosition.x >= Screen.width * 0.95) {
         cam.transform.position += cam.transform.right * (Time.deltaTime * movingSensitivity);
     }
     else if (Input.mousePosition.x <= Screen.width * 0.05) {
        cam.transform.position += cam.transform.left * (Time.deltaTime * movingSensitivity);
     }
 }

This will make your movement be relative to your cameras transform