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

Asp Net Core — Rest API Authorization with JWT (Roles Vs Claims Vs Policy) — Step by Step

Life changing ways to make use of Cypress

Event Base Architecture Using React

Some Fundamental React js Concept

Week 8 React

Isomorphic JavaScript is Dead… Long Live Isomorphic JavaScript!

JavaScript Internals: Prototypal Inheritance

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

Incongruity and Hijab

Four hours of Googling “international development”

Gates and the Epidemic of White Saviorism

Paradox Pair #28: Legacy & Heritage

Aeyon awarded a spot on the Joint Artificial Intelligence Center (JAIC) — Test and Evaluation…