How to move the player in the direction it is facing?

How to make the character controller move in the direction it is facing? I want to use transform.forward for this purpose.

using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class ThirdPersonController : MonoBehaviour
{
    [Header("Player Components and GameObjects")]
    public Animator playerAnimator;
    public CharacterController playerController;
    public Transform groundCheck;

    [Header("Movement")]
    public bool onGround;

    public float speed = 7f;
    public float gravity = -9.8f;
    public Vector3 velocity;

    public float xInput;
    public float jumpHeight = 2f;
    public float zInput;

    [Header("Rotaion")]
    public float currentVelocity;
    public float turnSmoothTime = 0.1f;

    void Update()
    {
        onGround = playerController.isGrounded;

        Movement();
    }

    void Movement()
    {
        if (onGround) 
        {
            velocity.y = 0f;

            xInput = Input.GetAxis("Horizontal");
            zInput = Input.GetAxis("Vertical");

            velocity.x = xInput * speed;
            if (Input.GetButtonDown("Jump") && onGround)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
            }
            velocity.z = zInput * speed;
        }


        velocity.y += gravity * Time.deltaTime;
        playerController.Move(velocity * Time.deltaTime);

        if (velocity.magnitude > 0.5f)
        {
            float targetAngle = Mathf.Atan2(velocity.x, velocity.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
        }
    }
}

Camera rotated controls (making sure “up” walks forward regardless of camera facing):

1 Like

I did not understand, plz explain that how to make the controller move based on the direction it is facing when wasd keys are hit.

Start with a tutorial for the kind of movement you want. There’s only a few basic choices / combinations, just choose the one you’re looking for.

If that doesn’t help you, absolutely NOTHING typed in this little text box will either.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

But still can you give me a hint to think upon to move ahead…

My first post linked you to a fully functioning demo project with comments… did you download and run the code linked in that project?

If so, what part of it is causing you issues?

Otherwise, what more hint are you looking for? I’ll post the link again in case you missed it:

Camera rotated controls (making sure “up” walks forward regardless of camera facing):

1 Like

Try using RigidBody set up like this:

I’ve copy and pasted this from some old stuff I still use as it just works simply .

Calling this function as CharController(controllerXvalue, controllerYvalue, button1, button2, button3)
attached to your player mesh with a RIGIDBODY on it, and use the new Input System to get keyboard/controller values as they’re just easier to deal with.

As Kurt says, you’ll have to follow tutorials if you want to understand the basics.


    public float playerSpeed = 1;
    public float currentSpeed = 1;
    public float playerWalkSpeed = 5f; // normal speed
    public float playerRunSpeed = 10f; // speed character runs
    public int rotatePlayerValue;
    [Range(0, 30)] public float turningRate = 8; // speed character turns to face other position

    public Rigidbody playerRigidbody;

    private Quaternion _targetRotation = Quaternion.identity;


void CharController(float x, float y, bool interactButton, bool throwButton, bool runButton)
{
    if (currentSpeed > playerWalkSpeed)
    {
        playerSpeed -= 10 * Time.deltaTime;
    }
    else
    {
        playerSpeed = playerWalkSpeed;
    }

    interactPressed = interactButton;

    functionPressed = throwButton;

    currentSpeed = playerSpeed;

    float angle;

    angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg; //work out angle using atan 2
    if (x != 0f || y != 0f)
    {
        _targetRotation = Quaternion.Euler(0, angle + rotatePlayerValue, 0);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, (turningRate * 100) * Time.deltaTime);
    }

    playerRigidbody.velocity = new Vector3(x * playerSpeed, playerRigidbody.velocity.y, y * playerSpeed);
    playerRigidbody.AddForce(transform.forward);

}