Are you new to Unity? Looking to learn how to take inputs from players and move characters around the screen? We've created this guide to showcase three different ways that you can control player movement in Unity.

Whether you're new to coding or have some experience under your belt, this C# tutorial will get you on your feet, and your characters on theirs.

Let's get started.

Player Movement in Unity: Collecting Inputs

First things first, you need to know how to capture inputs from the user and transform them into movement in-game. Unity makes this pretty easy as long as you know where to look.

Unity project settings

With Unity open, click on Edit at the top toolbar. Now select Project Settings. From the left-hand list, choose Input Manager. Select Axes, populating the list of input values. You'll want to look at Horizontal and Vertical for basic movement.

You'll use these axes along with Input.GetAxisRaw(); for basic movement in the next section.

Player Movement in Unity Using a Rigidbody and Collider

Now that you know the names of the axes, you can use them to control player movement.

In your Unity project's Hierarchy view, right-click and select 3D Object > Capsule to create what you'll bestow movement upon. Make sure to use the same tactic to create a ground Plane for your Capsule to stand on.

Make sure to reset the Transform value of both objects, and move your Capsule so that it's standing on the Plane. Rename your Capsule to something like "Player" for easy identification.

Related: The 5 Unity Game Development Languages: Which Should You Learn?

Click on the Player object and, in the Inspector view, scroll down to Add Component. Add a Rigidbody, and then add another component as a Capsule Collider this time. You'll need these components to add physics, and therefore movement, to your Player.

Then, right-click in your Scripts folder and Create a new C# Script. Name this script something along the lines of "PlayerMovement". If you're planning to add multiple types of movement for different characters or controller types, you'll want to create many different scripts for each type of movement. For now, we'll focus on the basics and use one script.

See also: What Is Modular Programming in Unity and Why Is it Important?

Double-click your script to open it. You'll be met with a default Unity script:

        using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

You can delete using System.Collections; and using System.Collections.Generic; as they're not needed for this script.

With that out of the way, focus on

        public class PlayerMovement : MonoBehaviour {
    

for now. Inside of the opening curly brace, create a public float variable named speed or something similar. This speed variable is a multiplier that will control how quickly our Player moves after some more programming. For now, set speed equal to something like 10f.

You also need to let Unity that there's a Rigidbody to manipulate in this script. This is done with the keyword Rigidbody and a variable name—we'll choose rb.

        public class PlayerMovement : MonoBehaviour
{
    public float speed = 10f; //Controls velocity multiplier
    
   Rigidbody rb; //Tells script there is a rigidbody, we can use variable rb to reference it in further script

That's all you need to add in this section. Now, move on to void Start(). Upon starting the game, you need to set the rb variable equal to the Rigidbody on the Player like so:

        void Start()
    {
        rb = GetComponent<Rigidbody>(); //rb equals the rigidbody on the player
    }

Now, take a look at the void Update() function. This is the function you'll use to constantly grab inputs from the players' keyboards, controllers, etc. Remember when you checked the Project Settings for Input Axes? You'll use them here.

        void Update()
    {
        float xMove = Input.GetAxisRaw("Horizontal"); // d key changes value to 1, a key changes value to -1
        float zMove = Input.GetAxisRaw("Vertical"); // w key changes value to 1, s key changes value to -1

        rb.velocity = new Vector3(xMove, rb.velocity.y, zMove) * speed; // Creates velocity in direction of value equal to keypress (WASD). rb.velocity.y deals with falling + jumping by setting velocity to y.


    }

Don't worry if you're feeling overwhelmed at the jump in code; we'll explain it step-by-step. First, create a float variable with a name like xMove, and set it equal to Input.GetAxisRaw("Horizontal");

Input.GetAxisRaw(); is Unity's way of recording Player inputs from the Axes you found in the Project Settings. You can read more about it in Unity's official documentation. "Horizontal" comes from the Horizontal Axis name in Unity. This axis controls left and right movement with the "a" and "d" keys.

As you can probably guess by now, float zMove = Input.GetAxisRaw("Vertical"); does the same thing but for the "w" and "s" keys.

Related: The Best Unity 3D Tutorials for Beginners

Next, you'll put that speed variable you created into play and complete the final piece of the puzzle for player movement in Unity.

        rb.velocity = new Vector3(xMove, rb.velocity.y, zMove) * speed; // Creates velocity in direction of value equal to keypress (WASD). rb.velocity.y deals with falling + jumping by setting velocity to y.

Head back into Unity's Inspector view for the Player object. Take a look at the Rigidbody—under Info, you'll see a value Velocity. This is the value you're targeting with rb.velocity.

new Vector3(xMove, rb.velocity.y, zMove) * speed, creates a new vector with the supplied x, y, and z values, then multiplies the value of the vector by speed.

Make sure to drag the PlayerMovement script onto the Player object in Unity, and that's it! Altogether, you have a working C# script that takes inputs from the player and transforms them into character movement in Unity.

Here's the finished code:

        using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 10f; //Controls velocity multiplier

    Rigidbody rb; //Tells script there is a rigidbody, we can use variable rb to reference it in further script


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>(); //rb equals the rigidbody on the player
    }


    // Update is called once per frame
    void Update()
    {
        float xMove = Input.GetAxisRaw("Horizontal"); // d key changes value to 1, a key changes value to -1
        float zMove = Input.GetAxisRaw("Vertical"); // w key changes value to 1, s key changes value to -1

        rb.velocity = new Vector3(xMove, rb.velocity.y, zMove) * speed; // Creates velocity in direction of value equal to keypress (WASD). rb.velocity.y deals with falling + jumping by setting velocity to y.


    }
}

Note: If you find your character doing more flopping around than anything else, make sure to lock the Player's rotation in the Inspector.

Learn and Make More in Unity

Now that you know how to program player movement in Unity with Rigidbody and Collider components, what will you learn next? Perhaps expanding on your movement controller to add things like sliding, double-jumping, and more would prove entertaining and engaging.

Unity Learn is chock-full of interesting topics to advance your game development skillset. We believe one of the most important factors in expanding your knowledge of programming is learning something new every day. Stay curious and happy hacking!