Simple platformer controls for 2D graphics in Unity

Let’s go over another take on platformer controls. This time for 2D graphics.

First, create an empty game object and set the name to Player. Drag in the first frame of the idle animation to the hierarchy and add as a child of the player object. Reset the position of the child object:

On the parent object add a Ridgidbody2D and a Box Collider 2D. Freeze the rotation on Z:

Create the Idle, Run and Jump animations:

Open the Animator window and add a speed and jump parameter. Set up the transitions between idle, run and jump:

Create a script called Player and add to the parent object:

Open up the Player script and add:
-Rigidbody2D for the rigidbody component on the player
-Two serialized floats: one for the jump height and the other for the movement speed
-Animator for the animator component on the child object
-Bool for checking if the player is on the ground
-Transform for the transform component on the parent object

Initialise Rigidbody2D, Animator and Transform in the Start function:

Create the functions for checking if the player is standing on the ground or jumping. For this script we use ray to check if the player is grounded:

Lastly, create the movement function. You can rotate the player either via the local Euler angles or the local scale:

Good luck!




Unity / C# Game developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top 10 javascript question

What is Javascript and some useful string method in Javascript

How to install Libindy for MacOS, with an Intel processor, to use with Aries Framework JavaScript.

redux-scc update: combined actions and custom types!

Resolving promises concurrently in JavaScript— The foolproof way

whenAll result

Add Angular 5 CLI project to ASP.Net Core 2.0 project

Guide to “Optional<T> Class” - Throw exception with Optional

Converting Devanagari Numbers to Arabic Numbers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Niklas Bergstrand

Niklas Bergstrand

Unity / C# Game developer

More from Medium

The 4th generation of university is the first of three generations inteversity

February 1, 2022: Felix Dictionary Begins Publication

Axelar covers a large spectrum of the community, the project is created for both developers and…