x


how to move player forward relative to camera forward with "mouse-y" input

hi! First of all im new to scripting. I have done intense searching in the unity answers, the forum and the script reference but i can't get a solution.. I use mouse-x and mouse-y input to move a 3rdperson player. (the x & y movement of the mouse should be translated to moving the character forward, backwards, left and right) Mouse-x also give a slight rotation so you can steer the player. But rotating doesn't influence the direction of the x and z axis. So when i rotate 180 degrees and I Make a forward input, the player just moves backwards along the original and unchanged z-axis

heres my script:

function Update () {

transform.position.x -= (Input.GetAxis("Mouse X") * 0.04);

transform.position.z += (Input.GetAxis("Mouse Y") * 0.04);

transform.rotation.y -= (Input.GetAxis("Mouse X") * 0.01);

}

I know it has something to do with transform.direction but from all the examples I stumbled upon, they all work with key input * speed as input, while i have raw numbers from the x and y movement of the mouse. My scripting knowledge is to narrow to make these things compatible. Can anyone push me in the right direction? Thanks in advance, any advice will be greatly appreciated! Laurens.

more ▼

asked Jun 08 '11 at 12:54 PM

laurens10 gravatar image

laurens10
1 3 3 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can get the Camera's forwrad direction by using:

Camera.mainCamera.transform.forward

Am I correct in assuming that you want to move the player along the camera's direction. So you would want to do something like this:

player.transform.position += Camera.mainCamera.transform.forward * mouseYDelta;

player.transform.position += Camera.mainCamera.transform.right * mouseXDelta;

This exact code will not work but it should give you an idea of what to do.

more ▼

answered Jun 08 '11 at 02:41 PM

RoflHarris gravatar image

RoflHarris
971 5 8 13

Yup, thank you for pointing it out! Script works like a charm now. I also fixed the rotation part. transform.rotation.y -= (Input.GetAxis("Mouse X") * 0.01); wouldn't let me steer more then 180° So instead I used eulerAngles which allowed me to go over 180° without running into issues.

Heres my working script:

function Update () {

transform.position -= Camera.mainCamera.transform.right *(Input.GetAxis("Mouse X")*0.03);

transform.position += Camera.mainCamera.transform.forward *(Input.GetAxis("Mouse Y")*0.03);

transform.eulerAngles.y -= (Input.GetAxis("Mouse X"));

}

Thanks again!

Jun 08 '11 at 10:17 PM laurens10
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x101
x83
x20
x2

asked: Jun 08 '11 at 12:54 PM

Seen: 3015 times

Last Updated: Jul 18 '11 at 09:01 PM