Smooth Platformer Player Controller in Unity

Making a smooth platformer player controller in Unity is pretty easy if you ask me. In this article, we will walk you through the process of creating a simple player controller script for a 2D platformer. By the end of this tutorial, you will have a player character capable of horizontal movement and jumping.

Smooth platformer player controller

Creating Platformer Player Controller:

Step 1: Set Up the Player GameObject

Start by creating a new 2D project in Unity. Create a player GameObject in your scene and connect a Rigidbody2D and a Box Collider 2D to it. This guarantees that the player object can interact with the physics engine and helps in making a smooth platformer player controller.

Note: You Can Now AI To Create Art Assets For Your Games, Click Here To Create Some

apply rigid body and box collider

Step 2: Create the Player Controller Script

Create a new C# script and attach it to the player GameObject. Give the script a name, such as “PlayerController”.

attach a script to make smooth player controller

And do remember to freeze the z-axis rotation in the RigidBody 2D component.

freeze z-axis rotation

Step 3: Reference the Rigidbody2D in the Script

Open the script and declare two variable, one to hold the reference to the Rigidbody2D component and the other for holding the reference of the sprite renderer component. Initialize this variable in the Start method to ensure it is set up correctly.

We’ll use the sprite render to flip the player direction according to the input.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;
    private SpriteRenderer spriteRenderer;
    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    
    // Other methods will be added here
}

Step 4: Implement Jumping

To allow the player to jump, use the Input.GetButtonDown(“Jump”) function to determine when the jump button is pressed. Set the Rigidbody2D’s velocity to an appropriate value to cause the player to jump.

void Update()
{
    if (Input.GetButtonDown("Jump"))
    {
        rb.velocity = new Vector2(rb.velocity.x, 10f);
    }
}

Step 5: Handle Horizontal Movement

Create a variable called horizontalInput to hold the player’s input along the horizontal axis. Use input.GetAxisRaw(“Horizontal”) delivers the raw input, which ranges from -1 to 1 depending on the player’s input.

Learn More About The input.GetAxisRaw function from Unity’s official Docs:

void Update()
{
    if (Input.GetButtonDown("Jump"))
    {
        rb.velocity = new Vector2(rb.velocity.x, 10f);
    }
    
    float horizontalInput = Input.GetAxisRaw("Horizontal");
}

Step 6: Apply Horizontal Movement

Determine the Rigidbody2D’s horizontal velocity based on the player’s input. This lets the player move left or right.

void Update()
{
    if (Input.GetButtonDown("Jump"))
    {
        rb.velocity = new Vector2(rb.velocity.x, 10f);
    }
    
    float horizontalInput = Input.GetAxisRaw("Horizontal");
    
    rb.velocity = new Vector2(5f * horizontalInput, rb.velocity.y);
    
}

Step 7: Introduce a Speed & Height Variable

Define a speed & height variable to control the player’s movement speed & jump height. Adjust the value according to your game’s requirements.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;
    public float speed = 5f;
    public float jumpHeight = 10f;
    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    
    void Update()
    {
        if (Input.GetButtonDown("Jump"))
        {
           rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
        }
        
        float horizontalInput = Input.GetAxisRaw("Horizontal");
    
        rb.velocity = new Vector2(speed * horizontalInput, rb.velocity.y);
    
    }
}

Step 8: Flipping The Player

We have almost completed our smooth platformer player controller but a simple yet important functionality is missing in our code which is flipping our player in the direction we are walking or facing.

So, we will implement that using our horizontal input variable, when the horizontal input is less than 0 which means our player is moving to the left then then we will set the sprite renderers flipX property to true.

And if the horizontal input is greater than zero which means the player is moving to the right then we’ll set the flipX property to false.

void FlipPlayer()
{
    if (horizontalInput > 0)
    {
        spriteRenderer.flipX = false;
    }
    else if (horizontalInput < 0)
    {
        spriteRenderer.flipX = true;
    }
}

Learn To Make TileMaps For Making Terrain For Platformers In Unity Here:

Now that you have implemented player movement the next thing you should do is implement player animations.

You Might Ask:

Do I need any prior Unity experience to follow this tutorial?

Basic Unity knowledge is recommended, but this tutorial is designed to be beginner-friendly.

Can I use this player controller script for 3D games?

No, this tutorial specifically covers the creation of a 2D platformer player controller in Unity.

How can I customize the jump height and movement speed of the player?

Adjust the velocity and speed variables in the script to meet your game’s needs.

What does Input.GetAxisRaw function do?

It retrieves raw input from the horizontal or vertical axis, returning a value between -1 and 1 based on player input.

Are there any best practices for optimizing player control in Unity platformers?

Consider looking through Unity’s documentation for further optimization suggestions and advanced capabilities to improve player control.