# Move player according to the camera direction

Hi, I want to be able to make my player move forward in different directions depending on what direction the camera is facing, so say if by default my player moved north when I pressed W (W = forward), how could I make it that if my camera was facing east my player would follow in the east direction instead of the north? I will list my Camera and Movement script files below…

Camera file:

``````[Header("Variables")]
public Transform player;

[Space]
public float camPosX;
public float camPosY;
public float camPosZ;

[Space]
public float camRotationX;
public float camRotationY;
public float camRotationZ;

[Space]
[Range(0f, 10f)]
public float turnSpeed;

//Misc
public static bool CamOrbit = true;

private void Start()
{
offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
transform.rotation = Quaternion.Euler(camRotationX, camRotationY, camRotationZ);
}

private void LateUpdate()
{
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
transform.position = player.position + offset;
transform.LookAt(player.position);
}
``````

Movement file:

``````//"rb" Is A Reference To Rigidbody
public Rigidbody rb;

//Internal Names To Change Values In Unity
public float forwardForce = 100f;
public float sidewayForce = 100f;
public float backwardForce = 100f;

//Controls For Player
void FixedUpdate ()
{
if (Input.GetKey("w"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime, ForceMode.VelocityChange);
}

if (Input.GetKey("a"))
{
rb.AddForce(-sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}

if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -backwardForce * Time.deltaTime, ForceMode.VelocityChange);
}

if (Input.GetKey("d"))
{
rb.AddForce(sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
``````

Try this: for each of your AddForce, make it so that you’re passing a whole Vector3 parameter instead of separate floats. Then pass them the main camera’s transform.forward/right multiplied by the amount of forces and Time.deltaTime. So, something like this:

``````// I'm just making variables for these things so our lines of code don't get too long
Vector3 forward = Camera.main.transform.forward;
Vector3 right = Camera.main.transform.right;

// The .normalized here is important
Vector3 forwardDir = new Vector3(forward.x, 0, forward.z).normalized;
Vector3 rightDir = new Vector3(right.x, 0, right.z).normalized;

if (Input.GetKey(KeyCode.W))
{
ForceMode.VelocityChange);
}

if (Input.GetKey(KeyCode.A))
{
ForceMode.VelocityChange);
}

if (Input.GetKey(KeyCode.S))
{