Move a player in the direction he is facing.

I have converted the survival shooter into a first person shooter, but am having difficulties getting him to go in the direction the camera is facing instead of the pos/neg x and z axes.

Here is my code, anyone have a good way to do this? I was thinking it should be something using translate instead of Vector3, but wasn’t totally sure how to do that without messing everything up.

`using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
%|1837095373_1|%
%|-1988135342_2|%
Animator anim;
%|573970496_4|%
%|1009415456_5|%
%|-1717068021_6|%

%|-416879092_7|%
%|-1063181321_8|%

%|-347868518_9|%
%|309713818_10|%
%|-1476747494_11|%
%|-1764302161_12|%
%|752330654_13|%
%|855801838_14|%

%|-20968000_15|%
%|1984206577_16|%
%|-824875282_17|%
float v = Input.GetAxisRaw (“Vertical”);

%|248628461_19|%
Turning ();
%|1311444592_21|%

%|-244052613_22|%

void Move (float h, float v)

%|2038457363_24|%
%|-1866289867_25|%
%|573705482_26|%
%|44033565_27|%

%|1202127834_28|%
%|-198277706_29|%
%|56915890_30|%
%|1040459942_31|%
%|1437352416_32|%
%|463237216_33|%

%|-1818838061_34|%
%|72649422_35|%
%|1364433722_36|%

%|960389714_37|%

%|1837724170_38|%
%|-1587850171_39|%
%|1586321773_40|%
%|-34726812_41|%

		Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

%|-742224437_43|%
%|-1571266492_44|%
}
%|83865066_46|%

%|1855510554_47|%
{
%|151747258_49|%
%|1140548255_50|%
%|1242713347_51|%
}
`
Thanks!

You need to move your object relative to the rigidbody’s local rotation with the help of Transform.forward and Transform.right.

Change your Move function to look like this and your character will move relative to it’s local rotation.

void Move (float h, float v)
	{
		movement.Set (h, 0f, v);
		movement = movement.normalized * speed * Time.deltaTime;

		playerRigidbody.MovePosition (transform.position + (transform.forward * movement.z) + (transform.right * movement.x));

		if (Input.GetKeyDown ("space")&& playerRigidbody.transform.position.y <= 0.8f)        //When you press space the player jumps, as long as he is on the ground//
		{
			Vector3 jump = new Vector3 (0.0f, 200.0f, 0.0f);
			playerRigidbody.AddForce (jump);
		}
	}