Tutorial for moving your player!

In this video, you will learn how to add a simple character controller in Unity. It will be very cool if you already have a movement script! Basically, the controller that has a simple green icon is a well known component in Unity projects and everyone knows how to use it – but not everyone knows how to add movement to your player and make it possible to jump. For that reason, this video has been created!


Click the video to watch.

Introduction

Character Controller has been a key to successful game design. This component gives your project another dimension. Instead of using Rigidbody that doesn’t work well on the slopes, this is a great component that makes movement very straightforward and simple!

tutorial for character controller

Gravity and Jump

The fastest and easiest way is to apply gravity, first define it as private float gravity and set it to 9.81, but in Unity it should be negative, because of inverted axes. After gravity, you need the velocity, because there is no predefined velocity in the character controller. It is important because you want your player to jump. This tutorial is a perfect entry for future game developers who had a harder time picking the right code. This code, finally, can be optimized for moving the character by using character controller in a 3D Unity project. The two variables – float gravity and Vector3 playerVelocity are defined and the script can be executed as following:

conn.SimpleMove(forceDirection * moveSpeed * Time.deltaTime);

It is the line of code that defines player movement with force direction, movement speed and multiplied with delta time. The player movement is very easy, but making the player jump is a more complicated task.

Apply Gravity

playerVelocity.y += gravity * Time.deltaTime;
conn.Move(playerVelocity * Time.deltaTime);

These two lines define the player velocity. The player uses SimpleMove for movement with force direction, but jumping only requires gravity and it is recommended to use Move and only define the player velocity with the delta time.

Check ground

if (isGrounded && playerVelocity.y < 0)
{
     playerVelocity.y = 0f;
}

Checks if the player is on ground and if the y velocity is smaller than zero. If that is the case, the player’s y velocity becomes zero, in which it won’t be able to move up or down if it is on the ground.

Check if you jump (spacebar)

public void OnJump(InputAction.CallbackContext ctx)
    {
        if (ctx.performed && isGrounded)
        {
            float jumpVel = Mathf.Sqrt(jumpSpeed * -2 * gravity);
            playerVelocity.y = jumpVel;
            isGrounded = false;
            Invoke("groundCoroutine", 2f);
        }
    }

This code was mentioned in previous videos, but it uses a different logic that describes the use of jump velocity and gravity. The square root has to multiply the jump speed with a negative number because that is where your player goes.

The player velocity is equal to jump velocity, which is predefined in the script. The isGrounded becomes false because the player is NOT on the ground anymore. The ground coroutine changes isGrounded to true after two seconds, which is when the player falls down.

Collider hits the ground

The last line of code includes OnControllerColliderHit, which is different from OnCollisionEnter because it detects only the collider.

private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        isGrounded = hit.normal.y > 0.9f;
    }

New Video Format

Like previous video, this video will also have the same format in which I will display how does my game look like on a phone. This format has been proven as successful and it easily brings the audience. The point is in simplicity and educational purpose, so people who want to learn how to use Unity can find something very interesting at the beginning of the video.

Fun Fact

8th February is the day on which Kozmobot has posted two videos: Respawn Player and Character Controller. In 2023, the channel had 170 subscribers, but in 2024, there are 770. That means exactly 600 subscribers have been gained for 365 days since the Respawn tutorial was published on my YouTube Channel. In order to celebrate, I have made a special image that collects these two thumbnails:

Conclusion

Unity Game Courses, also known as Unity Tutorials, are the best and most viable option for an indie game career. If this tutorial has helped you and you want to teach other people how to make games, share it with your best friends and family. More tutorials will come every week, so stay tuned to the new updates from the creator of Kozmobot and follow him on the social networks. The source code will be soon available on GitHub and will become available for all future game developers.


Leave a Reply

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