Move transform forward in relation to camera.

Hello i am trying to make a first person controller from scratch and i am stuck trying to make the player move forward in relation to the camera.

i have created a vector called forwardVector which should point towards where ever the camera is pointing.

but when i try to use it when pressing w nothing happens.

I hope you can help me, thanks!

using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour {

public float sensitivityX = 15F;
public float runSpeed = 10,
			 sprintSpeed = 20;
Vector3 forwardVector = Camera.main.transform.forward; 


// Use this for initialization
void Start () {
	Screen.showCursor = false;
}

// Update is called once per frame
void Update () {
	if (Input.GetKey("w")) {
		if (Input.GetKey (KeyCode.LeftShift)) {
			transform.position += Vector3.forward * (Time.deltaTime * sprintSpeed);
			Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 80, 2*Time.deltaTime);
		}
		else {
			transform.position += forwardVector * (Time.deltaTime * runSpeed);
			
			Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, 2*Time.deltaTime);
		}
	}
	if (Input.GetKey("s")) {
		transform.position += Vector3.back * (Time.deltaTime * runSpeed);
	}
	if (Input.GetKey("a")) {
		transform.position += Vector3.left * (Time.deltaTime * runSpeed);
	}
	if (Input.GetKey("d")) {
		transform.position += Vector3.right * (Time.deltaTime * runSpeed);
	}		
}
}

There are a few things wrong with this. First of all, you’re defining a vector called forwardVector, but you never actually use it. The program can’t read your mind - you have to tell it exactly what to do, and you’ve told it here to use the global forward vector (Vector3.forward), rather than the local forward vector of the camera.

Secondly, you can’t define the vector in the class definition like that, because the camera object won’t have been initialised yet - you’ll need to do it at least in Start.

Thirdly, because Vectors are value types, even if you did use the one you’ve defined, it will only work in the direction the camera was facing at the start of the game, because when you assign a Vector3, you essentially take a copy of it at that moment in time - any subsequent changes aren’t automatically copied over. You will be better off storing a Transform object reference to Camera.main.transform (since Transforms are reference types), and grabbing the forward vector directly from that.

You need to use Camera.main.transform.forward. Don’t save it locally because when the camera changes position, your local variable will not be updated. So use the camera’s variable directly whenever you need it.

using UnityEngine;
using System.Collections;
 
public class playerController : MonoBehaviour {
 
	public float sensitivityX = 15F;
	public float runSpeed = 10,
			  sprintSpeed = 20;

    private Transform cameraTransform;
	 
	 
	// Use this for initialization
	void Start () {
		Screen.showCursor = false;
        cameraTransform = Camera.main.transform;
	}
	 
	// Update is called once per frame
	void Update () {
		if (Input.GetKey("w")) {
		   if (Input.GetKey (KeyCode.LeftShift)) {
			 transform.position += cameraTransform .forward * (Time.deltaTime * sprintSpeed);
			 Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 80, 2*Time.deltaTime);
		   }
		   else {
			 transform.position += cameraTransform .forward * (Time.deltaTime * runSpeed);
			 Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, 2*Time.deltaTime);
		   }
		}
		if (Input.GetKey("s")) {
		   transform.position += Vector3.back * (Time.deltaTime * runSpeed);
		}
		if (Input.GetKey("a")) {
		   transform.position += Vector3.left * (Time.deltaTime * runSpeed);
		}
		if (Input.GetKey("d")) {
		   transform.position += Vector3.right * (Time.deltaTime * runSpeed);
		}   
	}
}