I want a player movement script for my project where you can control the camera view with the mouse. The direction of travel should change with the rotation, as in Minecraft.
Public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
void Update () {
yaw + = speedH * Input.GetAxis ("Mouse X");
pitch - = speedV * Input.GetAxis ("Mouse Y");
transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
if (Input.GetKey (KeyCode.W)) {
transform.Translate (0, 0, 10 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (0, 0, -10 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (-10 * Time.deltaTime, 0, 0);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (10 * Time.deltaTime, 0, 0);
}
}
Would be rejected for me.
Is completely unfriendly for the player if he can't change W, S, A and D (e.g. When playing with a controller).
It is better to use Input.GetAxis ("Horizontal"); / ("Vertical"); and multiplied by the values * Speed * Time.deltaTime. The values are -1 through 0 through 1.
However, the main question about turning is answered correctly here.
I don't even need the script anymore because I've seen this video