• Post category:Godot

In this godot 2d platformer tutorial using godot 3.1 we will look at how we can implement physics, 2d player animation, sound and pickups.

This same tutorial will also work for godot 3 if you have a slightly older version of godot.

If you are not keen on reading, I have created a video series on this here is the first video in the series.

Scroll down to the bottom of this post to get some of the other videos. Which are in this series from this tutorial.

If you don’t want to follow along and just want the source code for this game. You can get the source code here.

Everything you will need in order to create a fully fledged 2d platformer in godot.

To get started we need to create a few scenes.

One of which is the player scene which will hold our player as well as the logic which will take care of all the player movement and animations.

We also want to create a node which will hold our entire game which we will call the game node.

With this we will later be able to access our child game objects and scenes via that parent using gscript or code.

Here is what we will be building in this godot tutorial.

godot 2d platformer tutorial full scene example the game we will be building

You should end up with something like this at the end of this godot 2d platformer tutorial.

Setting up our nodes for our godot 2d platformer tutorial

If you rather just want to watch the video and not read here it is:

Some of the first things we want to setup is to rename our root node for our project to game so all our other objects can live under that. Like below.

Root game node for godot 2d platformer tutorial
Root game node

Next we want to create our player as a seperate scene so that we can manage our player independently from our scene. To do this goto the scene menu and click on new scene.

Godot create a new scene for our player - godot 2d platformer tutorial
Godot create new scene for our player

Once we have done this we want to import our player sprite into the scene. When that is done we need to align our player to the origin of our scene.

So that our player won’t spawn in all sorts of random positions.

This is important because if you don’t do this you might encounter some really random bugs in your project.

I created a basic 2d platformer character for including in our godot 2d platformer game. Here is what our character will look like.

2d platformer character - godot 2d platformer character
2d platformer character

To create our animations we need to create some frames we can work with. For this I will duplicate our character in inkscape and create a walk animation sheet. Before we do any of that let’s create a main game scene. To do that just click on this plus:


Create a 2D scene on left.

And rename it to Game for our root game object.

Resize your player in its scene to be much smaller so that it will fit comfortably in the camera view.

If you don’t know what the camera view is. Just check out the blue lines in this screen shot below. That’s where the screen view begins and ends.

Now you can save your player.tscn and we want to drag and drop our player.tscn into our Game.tscn

We now have our player in our main game scene. Let’s now look at animations we will now only be working within our player.tscn.

Creating a 2d character animation in Godot and setting up player scene

For our character animation we need to create a basic animation sheet.

Godot 2d platformer tutorial - Animating character sprites
Character animation sprite sheet

For us to create an animated character in godot. We are going to save each of the above images to separate images.

So we will call them idle, run and blink and save them as a png.

In order to use them in godot in our player scene.

We need to add a few new nodes, first is a kinematic body 2d, we don’t need this for the animation. However we do need it for controlling our player.

We need to add a 2d animation sprite node. We also need to add a collision shape 2d to allow for collision detection.

Here is how your setup should look.

godot platformer tutorial - kinematicbody2d
Add animated sprite in godot
Add godot collision shape 2d
Godot 2d platformer tutorial - AnimatedSprite, CollisionShape2D

Next we need to setup our colider for your our player.

In order to do this, we need to head over to our collisionshape2d node in the left scene inspector. Then we need to add a rectangle shape on the right.

Like so:

Select the new rectangleshape2d and we should now be able to resize it to fit our character.

godot platformer tutorial - character collisions detection

Godot Player Movement Scripts

We now want to just do a little bit of scripting so that we have some type of player movement.

In this script, we will deal with left and right motion as well as jumping and falling. So In order to get started create a new folder called scripts.

Right click and create a new script like below:

Name it PlayerMovement.gd script.

And finally hit create so we can start coding.

Open it up by double clicking on the script you should see something like this.

First thing you want to do is extend KinematicBody2D so that you can influence the body node of our player.

KinematicBody2D allows us to move our character around.

We will mainly be working in a method called _physics_process(delta) which allows us to focus on physics operations which are frame rate independent.

First we need to create some variables. Namely jumpvelocity, gravityscale, walkspeed and a velocity vector where we will store all our velocity changes.

First we just need to look at some basic input from the keyboard. So to keep things simple let’s just add left and right arrows to control a walk mechanic.

First function we want to define is one where we can get_input. So here is the function and the explanation to follow.

func get_input():
	if Input.is_action_pressed("ui_left"):
		velocity.x = -walkspeed
	elif Input.is_action_pressed("ui_right"):
		velocity.x = walkspeed
	else:
		velocity.x = 0

So input.is_action_pressed, looks at the predefined actions setup in our project. You can find these in Project -> Project Settings -> Input Map.

This makes it a little easier to do cross platform input in your games. You can map to actions instead of specific keys.

Then you can just change out the input map for other platforms. I would advise you to use this as much as possible.

So in our code we do an if statement to see if it’s ui_left or ui_right. We then add and subtract velocity to our x velocity value.

If neither button is pressed we are not moving so we just set our velocity to 0. This gives that nice responsive stop which most platformer games have.

Next what we do is we define our motion vector in _physics_process.

func _physics_process(delta):

	get_input()
	var motion = velocity*delta
	move_and_collide(motion)

We use get_input to get our input. We multiply our velocity by delta to make our movement smooth and frame rate independent and finally we call move_and_collide which will move our kinematicbody2d.

As well as make sure our collision2d node adheres to collision detection checks.

We now have our basic player movement script. Which we will expand on soon. Let’s attach our script to our player.

Click on the KinematicBody2D of the player and select to load the script.

load gdscript - godot 2d platformer tutorial
Godot 2d platformer tutorial - load player movement script

If you have done this you should now end up with a player which can move like this when you play your Game.tscn scene.

Godot 2d platformer tutorial player movement demonstration
Godot 2d platformer tutorial player movement demonstration

Adding gravity in our godot 2d platformer tutorial project

Let’s go back to our playermovement.tscn script. We want to now start to bring our gravityscale into play.

So first here is the settings I have for my variables. We will adjust these as needed but are a good starting point to get some gravity working.

var jumpvelocity = 150.0
var gravityscale = 200.0
var walkspeed = 150
var velocity = Vector2()

Now in our _physics_process we need to add the gravityscale.

func _physics_process(delta):
	velocity.y = gravityscale
	get_input()
	var motion = velocity*delta
	move_and_collide(motion)

Now you can run your game.tscn again to see the result. You should now have something like this.

godot 2d platformer tutorial - gravity scale

Great so we now have some basic physics working. Let’s now start creating a bit more of a scene.

We will use some of our assets from our inkscape project to at least create a floor for our player to stand on.

Creating platforms for our godot 2d platformer

If you don’t like reading here is the video version of the below text.

I am now going to export it as our ground.png file which we will bring into our godot 2d platformer tutorial project.

Export platform for our godot 2d platformer tutorial project

Drag in the ground into our game scene like so.

platform image  - godot platformer tutorial

Add a child node.

Add a collisionshape2d node as a child to our ground.

You will now see this yellow warning sign.


That’s because we need to allow for a shape in our collisionshape2d. Just like before on the right we will need to select a shape.

New RectangleShape2D fits our ground so is the most logical shape to use. Resize it like we did with our player.

Setting ground collision for our godot 2d platformer game

Now we can try play our main scene again to see if our player stays on the floor after falling.

You might notice that this didn’t quite work. The reason is that godot needs a body on the other object namely our ground in order to interact.

So we add another child node for our ground called StaticBody2D. Then we move our collisionshape2d so that it is a child of staticbody2d. Like below.

Now if you play your scene you should end up with this.

Godot 2d platformer tutorial - collision detection not working fixed

Awesome so now we have collision detection with our floor. Let’s make our scene a little better looking lets export our background from Inkscape and add it to our game.tscn.

Export platformer background

You should now have something that works like this.

Godot animations in our platformer

Here is what we will end up with.

Godot 2d platformer animations

It is pretty simple to achieve the above. We just need to make some changes to our script.

Something to note is to make sure you use the correct names of your animations like we defined them earlier in this tutorial.

Otherwise you might end up with a weird bug where you player disappears off screen.

For our code we just need to add 3 new lines. We will use the short hand $ sign to get our AnimatedSprite.

So under our ui_left and ui_right blocks we want to add $AnimatedSprite.play(“run”) and under our else where we not moving change our animation to idle.

We can do that by running $AnimatedSprite.play(“idle”). Your code should look something like this below.

func get_input():
	if Input.is_action_pressed("ui_left"):
		$AnimatedSprite.play("run")
		velocity.x = -walkspeed
	elif Input.is_action_pressed("ui_right"):
		$AnimatedSprite.play("run")
		velocity.x = walkspeed
	else:
		$AnimatedSprite.play("idle")
		velocity.x = 0

Let’s also quickly add the ability to jump. You can tweak your jump velocity and gravity scale to your needs.

However all we need to add our player jump is to create a new input map called player_jump like so:

Then add to our get_input method the following in our player movement script:

	if Input.is_action_pressed("player_jump"):
		velocity.y-=jumpvelocity

You should now have something like this:

We will later adjust our jump to block double jumps. For now you have a player which can walk and jump and is animated. In the next part we will do some level design.

Level design lets add some platforms

So again back into our inkscape project let’s create some variations of platforms and add them to our scene.

I also created some coins for a coin pickup in our platformer. The item pick up is a really common mechanic in platformers so let’s add some coins to our level as well.

Then in the next section we will add some code to make our coin pickup actually work.

So in order to make this more generic we want to add our coin in godot as a new tscn so that we can modify it globally when we need to change anything.

So go ahead and create a new scene by clicking the plus on the top tab like before.

Drag your coin into the scene and placing it at the origin of the scene like so:

Save the scene as coin.tscn.

Let’s now create a platform scene as well. So that we have a centralized place to control a platform.

This especially will be useful if we want to generate an infinite world with random platform positions.

Make sure to add a collision2dshape to it like below.

Also don’t forget to add a static body on top of your collisionshape2d node like below.

Now save it as platform.tscn and lets start adding our coin and platform to our main game scene.

You should now end up with something like this.

Godot 2d platformer tutorial - item or coin pickups

Arrange your level the way you like. You should now be able to jump up on platforms and navigate your level.

We next want to look at coin pickups and how we can do them in our platformer game.

This will take some gdscript but shouldn’t be too difficult to achieve. We also will look at some basic UI on keeping track of our coins we have picked up.

Fixing our walk speed for our player movement script

So before we move on to the next section let us just address the elephant in the room.

You may have noticed that there is friction on your character when you walk. We are going to change some fundamental things about our player movement script, but don’t worry it’s nothing difficult.

Let us set our walkspeed to something more reasonable like 350.

var walkspeed = 350

Let’s also change our _physis_process method to use move_and_slide instead of move_and_collide like so:

func _physics_process(delta):
	get_input()
	velocity.y = gravityscale
	move_and_slide(velocity)

You will notice this makes the player’s walk a lot smoother and the jumping also makes more sense.

Fixing the player direction flipping the player on the horizontal axis

One final thing to look at is our players facing direction. In order to fix this we need to have a new variable called movingright. This will basically just tell godot if our player is moving right or not.

So first you need to add this to your player movement script.

Then under our two actions we want to set our movingright variable to be false or true depending on when we moving right or not. Like this:

Godot 2d platformer tutorial - player direction and flip on horizontal axis code

Once you have done that. We simply need to check which direction we moving in and flip our player in that direction on the horizontal axis.

We usually do this on the sprite so in order to do this we have to add this code.

	if not movingright:
		$AnimatedSprite.set_flip_h(true)
	else:
		$AnimatedSprite.set_flip_h(false)

You should now have a player which runs, changes direction and jumps correctly. We now ready to look at item and coin pickups.

Godot 2d platformer tutorial: UI for our item or coin pickups

First let’s create a UI to display the number of coins we have picked up.

We will just create a basic counter in our scene which we can use you should at the end, end up with something like this.

Godot 2d platformer game tutorial - UI

Create a new new scene so we can manage our UI elements in that scene. You will then need to add a node called NinePatchRect. Add that to your scene.

In the Texture slot on right drag in a texture of your choice. For this tutorial I have just dragged in a platform image to use for our UI.

Once you have done that add two child nodes to your NinePatchRect one called a label and another called TextureRect.

Arrange them like this and and name them appropriately.

You should now be able to arrange your icon and label as needed. I arranged mine like so:

Godot 2d platformer tutorial - Coin label and icon

I added some place holder text in my Label so that we have something to see.

Save your new UI.tscn and drag it into your game scene so that it looks like this.

Godot 2d platformer tutorial - How the UI will look
Godot 2d platformer tutorial – creating UI/GUI in godot

You should now have quite a decent looking platformer with UI, coins, background and a player which can move around the scene.

We now need to look at coin pickups and how we can modify our coin counter.

Creating the item or coin pickup script for our godot platformer game

So in order to allow our player to pick up coins we need to detect collisions on coins. So let’s start by going back into our coin.tscn and start by adding a Area2D and a collisionshape2d.

This time we will use a circleshape2d.

Resize the shape collider.

Godot 2d platformer tutorial - how to create a coin or item pick up in godot
Godot 2d platformer tutorial – how to create a coin or item pick up in godot

We now need to create a script which will allow us to detect the collision and do something with that.

So in your scripts folder create a script called item pickup.

Add your script to your coin area2d.

Godot 2d platformer tutorial  - Creating a coin with area2d item pick ups

In order to make our script work we need to re arrange our nodes as follows:

So godot has a great tool which we can use to add some predefined functions or methods. Let’s first just start by extending our area2d in our script. Like so:

Now you want to head over to Area2D or your CoinArea on the left.

On the right click on Node.

You should see this:

Double click on body_entered. Click on CoinArea and connect.

This will now generate this code for you.

Godot 2d platformer tutorial - Area2d using signals in godot

Remove pass from your code and call queue_free() this essentially will delete the body if some enters it’s space.

Hit play and you should now see that when your player touches a coin is disappears.

So now the next thing we need to do is keep some score.

So let’s first start by going back to our player movement script and add a variable called score.Like so:

Godot 2d platformer tutorial - Keeping player score

Now the tricky part is because we have separated our coin and our player into separate scenes we need a way to bridge their instances.

For this we will add a global gdscript to help us control this situation.

So go ahead and create a script called GameManager in your scripts folder.

Godot 2d platformer tutorial - connecting signals multiple scenes
Godot 2d platformer tutorial – connecting signals multiple scenes

Now to get our gamemanager being preloaded. Goto Project->Project Settings and Auto Load.

Click on add and add your gamemanager script.

Godot 2d platformer tutorial - add auto load gdscript

Let’s head back to our GameManager script and add a variable to hold our player instance. So simply create a variable called Player.

Godot 2d platformer tutorial - Player in our game manager script

Once you have that you want to head over to your Player Movement script and in the _ready method set your player like so.

This helps you make the connection. Let’s just add a function in our Player movement script which can increase our score.

Great so now finally we need to make the connection. So in our Coin tscn we want to go over to CoinArea again go to our code where we are destroying our coin and add the following code.

Great so let’s hook this up to our UI. So let’s in our GameManager add another variable called coinUI.

We now need to connect a signal to our coin UI. So in order to do this we need to add a coin script. Attach it to the CoinCounter.

Add this code to the script:

Game manager assigning UI or GUI in godot

Now all we need to do is add a check in our increaseScore method on our player and we will be able to update our score.

So to do this let’s now head over to our Player Movement script again and update our label with our score.

Go into the increase score function and add the following code.

Godot 2d platformer tutorial - increase score for coin pickup code

So this basically loops over the children of our main ui node and checks if it’s a label. If a label update the text and cast the score to a string for our label test.

If you have done all this correctly you should end up with something like this.

Godot 2d platformer tutorial - coin / item pick up , jumping, platforms, background, player animation

Godot platformer tutorial – Adding sound

We are going to generate some audio for our jump and coin pickup using SFXR.

adding sound to godot platformer game

Grab SFXR and generate some sounds and export them as .WAV.

So to add sound is extremely easy. Let’s head over to our Player.tscn. Add a new node to our Player called AudioStreamPlayer2D.

Go to stream on the right.


Click on load and load your jump.wav file. Now head over to our player movement script and under the portion where we do the jump. We just need to play our sound like this.

Let’s do the same for our coin pick up. So in our coin.tscn we want to add another AudioStreamPlayer2D.

Link your coinpickup.wav and in our ItemPickup script add the following code.

This method is a bit basic, you may want to add these to a AudioManager global script to manage your sounds, so that you can play them from anywhere in your games scenes.

However, this gives you the understanding of how you can play sound and then you can go build your own sound manager much like the game manager we created earlier.

There you go you have a fully functional godot 2d platformer. With sound, player animation, coin or item pickups and more.

General tips

  1. If you have maybe created your scenes in the wrong order you might end up not being able to play your main scene. You can easily fix this by selecting your main scene in godot by going to Project =>Project Settings=>Application=>Run=>Main Scene.

Other useful resources

If you are not looking to build a godot 2d platformer from scratch and want to rather use a godot 2d platformer template. Then you can get the source code for this project here: Godot 2d platformer source code

If you are looking for more godot tutorials why not check out some of my previous godot tutorials here is one on Godot 2D top down movement tutorial.

You can check out the video of my Godot 2d top down movement tutorial here: Godot 2d top down movement tutorial .

Another useful godot resource is my YouTube channel : Generalist Programmer Youtube Channel

Final words

If you liked this tutorial please share this tutorial on social media, consider subscribing to my YouTube channel and hitting the bell so you are notified of any future videos.

Also consider checking out my skillshare courses, if you join skillshare with my affiliate link you support me so that I can make more in depth tutorials such as these.

Thank you for taking the time to read and see you in the next tutorial.

Some other articles you might be interested in: