Game Creation Mastering Development

Unity – move with WASD in the way the camera is facing

I have this C# code for rotating the camera with the mouse:

public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;

private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis

void Start()
    Screen.lockCursor = true;
    Vector3 rot = transform.localRotation.eulerAngles;
    rotY = rot.y;
    rotX = rot.x;

void Update()
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = -Input.GetAxis("Mouse Y");

    rotY += mouseX * mouseSensitivity * Time.deltaTime;
    rotX += mouseY * mouseSensitivity * Time.deltaTime;

    rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

    Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
    transform.rotation = localRotation;
    transform.parent.transform.rotation = Quaternion.Euler(0.0f, rotY, 0.0f);

The script is on the camera, and camera object is a child to the player object.
Now I want to move the player object in the direction the camera is facing, how do I do this?

Leave a Reply

Your email address will not be published. Required fields are marked *