Move player forward with mouse relative to camera

I am trying to get my character to move forward relative to the y camera rotation. I am working with the oculus rift using a mouse as the input. I’ve done this:

			if (Input.GetAxis("Mouse X")<0) moveLeft =true;
			if (Input.GetAxis("Mouse X")>0) moveRight =true;
			if (Input.GetAxis("Mouse Y")<0) moveBack =true;
			if (Input.GetAxis("Mouse Y")>0) moveForward =true;
			if (Input.GetKey(KeyCode.KeypadMinus)) Acceleration = Acceleration - .005f;
			if (Input.GetKey(KeyCode.KeypadPlus)) Acceleration = Acceleration + .005f;
			// Arrow keys
			if (Input.GetKey(KeyCode.UpArrow))    moveForward = true;
			if (Input.GetKey(KeyCode.LeftArrow))  moveLeft 	  = true;
			if (Input.GetKey(KeyCode.DownArrow))  moveBack 	  = true; 
			if (Input.GetKey(KeyCode.RightArrow)) moveRight   = true; 


			if (moveForward)
				MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence);

			if (moveBack)
				MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence);

			if (moveLeft)
				MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence);

			if (moveRight)
				MoveThrottle +=  DirXform.TransformDirection(Vector3.right * moveInfluence);

The keys work properly, however the mouse movement moves the character on a locked x and y axis no matter the camera rotation. I am very poor at coding so please explain thoroughly.

using Vector3.forward or Vector3.right, Vector3.left, etc, are all WORLD directions. So Vector3.forward will always be z positive according to the world coordinates.

instead use





Replace all of your Vector3 directional code with references to the camera.
You can access the main camera (the camera must be tagged as “Main Camera” in the inspector. When you start a new scene, the first camera defaults to this tag) through that line of code:

Camera (the camera class)

Camera.main (a static reference to the camera that is tagged as “Main Camera”)

Camera.main.transform (the transform, i.e. the object that contains all position and rotation info, that is attached to the main camera object)

Camera.main.transform.forward (the “forward” direction that is relative to that transform, or basically the direction the camera is facing)

hope that helps