- Home /
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.
I think the use of books may be causing it. Use two floats, one for mouse x and one for mouse y. Then make a dead zone control, so it Is not too sensitive. $$anonymous$$ove by using the floats multiplied by altitude.
Answer by zombience · Dec 31, 2013 at 01:54 AM
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
Camera.main.transform.forward
and
Camera.main.transform.right
etc.
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
for comments like this, post a new "comment", not an "answer" also, if the question is answered, don't forget to mark it as answered.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125207im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vertical mouse input inverted when player is turned around 0 Answers
Rotating GameObject Around Player With "Mouse Y" Position 2 Answers
Trying to make a Point system that references a Prefab. 1 Answer
Rotation of Object on single axis in direction of the mouse position 0 Answers
Slerp/Rotational Problem Query 1 Answer