Third person controller : How to make player to move towards the direction the camera is facing ?

Hello.

I am trying to create my own third person controller.

I have managed to get basic movement , but I am stuck at the direction the player will move.

I have managed to get the player rotate towards the direction the camera is facing, but I don’t know how to move make it move towards the direction the camera is facing.

Anybody knows how to fix this ?

Here is my code for the movement

  public float movementSpeed;

    public float walkSpeed;
    public float runSpeed;

    bool running;
    CharacterController charController;
    private void Start()
    {
        charController = GetComponent<CharacterController>();
    }

    private void Update()
    {
        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        running = Input.GetKey(KeyCode.LeftShift);

        Vector3 inputDir = input.normalized;

        if (running)
        {
            movementSpeed = runSpeed;
        }
        else
        {
            movementSpeed = walkSpeed;
        }

        Vector3 moveAmount = inputDir * movementSpeed;

        charController.Move(moveAmount * Time.deltaTime);

        Vector3 viewDir = Camera.main.transform.forward;

        viewDir.y = 0;
        viewDir.Normalize();

        Quaternion newRot = Quaternion.LookRotation(viewDir);

        // player transform
        transform.rotation = Quaternion.Slerp(transform.rotation, newRot, 5f * Time.deltaTime);
     
    }
}

and Camera movement

  public bool lockCursor;
    public float mouseSensitivity = 10;
    public Transform target;
    public float dstFromTarget = 2;
    public Vector2 pitchMinMax = new Vector2(-40, 85);

    public float rotationSmoothTime = .12f;
    Vector3 rotationSmoothVelocity;
    Vector3 currentRotation;

    float yaw;
    float pitch;

    cameraCollisions cameraCollisions;

    Vector3 newCameraPosition;
    void Start()
    {
        cameraCollisions = GetComponent<cameraCollisions>();
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    void LateUpdate()
    {
        yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
        pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
        pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

        currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
        transform.eulerAngles = currentRotation;

        if (!cameraCollisions.hasHitWall)
        {
            transform.position = target.position - transform.forward * dstFromTarget;
        }

      
    }

}

The problem is that your just taking the input axis and making that into a vector. What you would typically do is multiply the input axis by the characters forward and right vectors like this:

Vector3 moveDir = transform.right * inputX + transform.forward * inputY;
1 Like

Yes ! It worked.
Thank you. You saved my whole project.

Can you please show this being applied in Script???

1 Like