0% found this document useful (0 votes)
441 views99 pages

Godot Game Development For Beginners (001 099)

This document provides an introduction to creating a 2D platformer game using the Godot game engine. It discusses installing Godot, creating a new project, exploring the editor interface, and how games are constructed in Godot using scenes and nodes. The document then begins explaining how to set up the player, tiles, enemies, and other game elements to build out a basic 2D platformer game.

Uploaded by

Vinicius Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
441 views99 pages

Godot Game Development For Beginners (001 099)

This document provides an introduction to creating a 2D platformer game using the Godot game engine. It discusses installing Godot, creating a new project, exploring the editor interface, and how games are constructed in Godot using scenes and nodes. The document then begins explaining how to set up the player, tiles, enemies, and other game elements to build out a basic 2D platformer game.

Uploaded by

Vinicius Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 99

Godot Game Development

for Beginners

By Daniel Buckley
Godot Game Developer

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Table of Contents
How to Make Your First Game with Godot
Introduction
Project Files
Installing Godot
Creating a New Project
Exploring the Editor
How Godot Works
Creating our First Scene
Creating the Player
Scripting the Player
Creating a Tile
Creating an Enemy
Colliding With the Enemy
Collecting Coins
Tracking Camera
UI
Scripting the UI
Conclusion
Create a First-Person Shooter in Godot - Part 1
Introduction
Project Files
Let's Begin
Building Our Environment
Creating the Player
Scripting the Player
Creating the Bullet Scene
Shooting Bullets
Creating the Enemy
Continued Part 2
Create a First-Person Shooter in Godot - Part 2

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Introduction
Project Files
Scripting the Enemy
Player Functions
Pickups
UI
Scripting the UI
Conclusion
Build a 2D RPG in Godot - Part 1
Introduction
Project Files
Setting Up The Project
Creating the Player
Scripting the Player Movement
Animating the Player
Creating the Tilemap
Continued in Part 2
Build a 2D RPG in Godot - Part 2
Introduction
Project Files
Camera Follow
Creating an Enemy
Scripting the Enemy
Player Functions
Player Interaction
Chest Interactable
Creating the UI
Scripting the UI
Conclusion
Develop a 3D Action RPG in Godot - Part 1
Introduction

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Project Files
Project Setup
Creating Our Environment
Creating the Player
Camera Look
Scripting the Player
Gold Coins
Continued in Part 2
Develop a 3D Action RPG in Godot - Part 2
Introduction
Project Files
Creating the Enemy
Scripting the Enemy
Sword Animation
Attacking the Enemy
Creating the UI
Scripting the UI
Conclusion
Make a Strategy Game in Godot - Part 1
Introduction
Project Files
Setting up the Project
Creating the Tiles
Creating the UI
Tile Script
Map Script
BuildingData Script
Continued in Part 2
Make a Strategy Game in Godot - Part 2
Introduction
Project Files

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Finishing the Map Script
GameManager Script
UI Script
Connecting Everything Together
Conclusion

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
How to Make Your First Game with Godot

Introduction

Are you eager to get started making your own games?

Game development has never been more popular before - with sales stretching into the billions,
and thousands of indie developers sharpening their skills and bringing their creations to life.
Even if you're a beginner who has never coded before, with numerous engines available, just
about anybody can create and program their dream game project.

In this comprehensive tutorial, we're going to be learning how to create your first game using
Godot, a free, open-source game engine that allows you to create 2D and 3D games. Due to its
open-source nature, which means users can add and remove things from the engine at their
leisure, it has quickly been gaining vast popularity. With a vibrant community ready to assist, it
is a perfect choice for creating your first game.

After installing Godot and learning the basics of the editor, we're going to be creating a 2D
platformer game, so strap yourself in and get ready to start developing games!

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Project Files

For this project, we're going to be needing a few assets for the player, tiles, coins, enemy, etc.
You can create your own or use the ones featured in this tutorial. The assets can be
downloaded here.

You can also download the completed Godot project via the same link!

Installing Godot

To download and install Godot, let's go over to https://godotengine.org/. Here, you can view
Godot's features, community pages and more. We want to click on the Download button.

Select your platform, then download the 64 or 32 bit standard version (depending on your
operating system).

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
This will download a .ZIP file. Inside of that is an application which you can extract to anywhere
on your computer, and that's it - Godot is installed.

Creating a New Project

Open up the Godot application we download to see the Project Manager. Here, we can create
projects, view others and download templates.

Click on the New Project button to create a new project.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
This will open up a new window.

1. Enter in a name for your project


2. Click the Create Folder button to create a new folder for the project in the Documents
folder
3. Click the Create & Edit button to launch the engine and begin creating the game

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Exploring the Editor

When the editor pops up it may look pretty daunting with all the buttons and options, but let's
break it down.

Godot has 4 main panels which we'll be using to create our game and each serves a specific
purpose.

1. This here is the Scene panel. It will display the node layout and hierarchy of the scene
we're currently in. We'll go over what a scene and a node are very shortly.
2. The FileSystem panel shows us all of the assets and files we have. Sprites, models,
scripts, scenes, folders, audio, etc.
3. This is where we can see and create our game. Moving things around, selecting,
scripting, etc. Above this panel are four buttons and they toggle what the panel
becomes. We can switch between 2D and 3D modes, the script editor and external
asset library.
4. This is the Inspector and this shows us the details of a node when we select one. The
position, rotation and any other attributes which we can modify.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
How Godot Works

Above we were talking about scenes and nodes. What are they? Well, a game in Godot is made
up of a hierarchy of nodes. A node can be anything: a player, camera, 3D model, light, UI, etc.
Nodes make up all of the entities in your game and also have the ability to be a child of another
node.

Here's an example of a player in a game. First, we have the KinematicBody node which can
control movement and some physics interactions. Then, as a child of this, we have a collider
and sprite node. When the kinematic body node moves, rotates, etc, the others will follow. Even
if we delete the parent node, the children will follow.

Games in Godot are made up of many parent-children nodes to create the various different
elements and systems of a game.

Since a game is made up of nodes, it will eventually get to a point where there are hundreds or
even thousands of them in the scene tree panel. This will make it hard to find certain ones and
overall make working on the game confusing. To solve this issue, we can divide up our nodes
into scenes.

Scenes are self-contained node packages which we can then drop into other scenes as nodes.
Let's take our player example and turn that node hierarchy into a scene. It is a saved file in the
FileSystem which we can then drag into another scene.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
A benefit of dividing your game up into nodes is also the fact that we can remove the need to
repeat common node structures. Instead of having 100 tile nodes which all have a sprite,
collider, etc all in the same scene, we can just create one of those as a scene and drag in
multiple instances.

We'll be exploring nodes and scenes throughout this project.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Creating our First Scene

Alright, let's get started on our 2D platformer project. First, we'll need to create a main scene
which will be the basis for our game, containing other scenes such as the player, tiles, coins,
and enemies.

In the scene panel, click on the 2D Scene button to create a new 2D scene.

With our new node, let's double click it and rename it to MainScene.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
It's also good to save your scenes when you create them. Save with either Ctrl + S or Scene >
Save Scene. Hit enter and you should see it down in the file system.

Creating the Player

Alright, we got our main scene. Next, let's go and create our player scene which will hold all the
nodes we need and the corresponding script.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
To create a new scene, we can go Scene > New Scene. In the scene panel, select Custom
Node. This will open a window and we want to create the KinematicBody2D node.

Rename the node to Player, then save it.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
With our platformer game, we'll be needing a few sprites and other assets. Included with this
tutorial, is a .ZIP file containing all the assets we'll need. Download that and drag the three
folders into our Godot project folder.

Back in Godot, let's right-click on the Player node and select Add Child Node. We want to add
in a CollisionShape2D node as a child. Then in the file system, find the Player_Idle image and
drag that into the scene window to add that as a child node. Rename that node to Sprite.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We want our sprite to be centered, so select it and over in the Inspector:

• Open the Transform drop-down


• Set the Position to 0, 0

You might see that the collision node has an error symbol next to it. This means we need to give
it a shape. Select the collision node and in the inspector set the Shape to Capsule. To make it
visible, re-order the node hierarchy by dragging the collider node underneath the sprite node.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
To edit the collider properties, select the shape in the inspector and it will open up more options.

• Set the Radius to 27


• Set the Height to 12
• Set the Position to -0.5, 14.7

Scripting the Player

Now that we have the node structure setup, we can begin to script our player. This will involve
the movement, jumping and collecting coins.

To create a script, select the parent Player node and in the Inspector, create a new script. This
will open up another window, just hit enter.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
What you'll see then is that the main window will switch from 2D to Script mode.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Godot uses its own scripting language called GDScript. This is similar in syntax to Python. We
won't be going over every aspect of the language or the basic concepts of programming - so a
basic understanding is required going forward.

Right now in the script we have two things.

Extends is similar to using or import. We're extending from the kinematic body 2D object we're
attached to, so we'll have direct access to those attributes.

This is a function that is basically a block of re-usable code we can call. The _ready function is
built into Godot and gets called once when the node is initialized.

The pass is simply a filler line to define the function. If it's empty then an error will occur. The
pass does nothing.

We're going to start by adding in some variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
A Vector2 defines an x and y value which can be used for position, scale, rotation, velocity.
We're going to be using it to store our player's current velocity.

We need one more variable. This is a reference to the sprite component which is a child
node. onready means that we're going to find the Sprite node when the node is initialized.

Now that we have our variables defined, let's start to get our player moving. To do this, we first
want to define some key inputs. Open the Project Settings window (Project > Project Settings)
and in there, navigate to the Input Map tab.

In the Action field, enter in a name for the input and click add.

We want to create 3 new actions.

• move_left
• move_right
• jump

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Next to each action is a '+' icon. Click that, then select Key. This will allow us to enter in a key
for that action.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Enter in a new key for each of the three actions.

Once that's done, we can close the window and continue scripting.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We're now going to create a function which is built into Godot. The _physics_process function
gets called 60 times a second and is what we use for physics calculations. We can remove
the pass once we start filling the function in.

The delta parameter is the time between each frame. We can multiply our movement by this in
order to move based on pixels per second, rather than pixels per frame.

First, we want to reset the horizontal velocity. Then check for the left and right movement key
inputs. These will change the horizontal velocity.

Next, we'll move the player using the move_and_slide function which will move along a certain
velocity, detecting colliders and other things. The second parameter is the ground normal (which
way is the ground pointing?).

After this, we can apply gravity and check for if we're pressing the jump button and on the floor.
If so, jump.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Finally, we can flip the sprite depending on which way we're moving.

We're done scripting for now, so let's switch back to 2D mode and go to the MainScene. Here,
we want to drag in the Player.tscn scene from the file system.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
What we can do now is test the game out. In the top right corner of the screen, click the Play
button.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
A window will pop up saying that there's no main scene selected. Click the Select button and
select the MainScene.tscn scene as the default one.

The game should then open up but we'll quickly fall off-screen due to gravity. What we need to
do is create a tile scene which we can then duplicate in the main scene.

Creating a Tile

Let's now create a new scene (Scene > New Scene), select Custom Node and search for
the StaticBody2D node. This is a physics node which is static so it doesn't move or do anything
but detect collisions.

1. Rename the node to Tile


2. Attach a new CollisionShape2D child node
3. Set the Shape to Rectangle
4. Set the Extents to 32, 32
5. Drag in the Tile image from the file system to create a new sprite node
6. Rename it to Tile
7. Set the Position to 0, 0
8. Save the scene as Tile.tscn

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Back in the MainScene, we can drag the tile scene in. Then duplicate it with Ctrl + D and move
it around.

To make it easier to position the tiles, we can turn on snapping. At the top left of the scene
panel, click on the snap button to toggle it. Then click on the three dots to bring up the snap
settings. Make sure only Use Pixel Snap is selected. Now when we move a tile around, it will
snap based on the pixels.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Now we can press play and test it out on our new level.

Creating an Enemy

Create a new scene with a root node of Area2D. As a child attach a collision shape 2D node in
the shape of a circle and drag the Enemy sprite in too (make sure to center it). Then save the
scene as Enemy.tscn.

Select the Area2D root node and create a new script called Enemy.

First, we want to create the variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
export means that this variable is exposed in the inspector, allowing us to change the property
on the instance of the scene.

Then in the _physics_process function, we're going to be moving between the startX and
targetX positions. The move_to function is our own custom function we'll make after.

The move_to function translates a given value to a target based on a given step.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Back in the MainScene, drag the Enemy.tscn into the scene. Figure out the distance between
the enemy and where you want to move to and set that in the Move Dist property.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We can now press play and see the enemy in action. You may notice that it's quite slow, so let's
set the Speed to 500.

Colliding With the Enemy

Right now, the player can walk through the enemy with no issue. What we want, is for the game
to reload if we hit the enemy. To do this, we need to work with signals. These are events that
can be called when a certain thing happens to a node.

In the Enemy scene, select the root Area2D node and in the Inspector, switch to the Node tab.
Here, we can see all the signals that the node can emit. We want to double click on
the body_entered signal. Press enter and it should create a new function in the Enemy script.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In this function, we're going to check if the body we entered was called Player. If so, call the die
function on that node.

Let's now go to the Player script and create the die function.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Now when the player hits the enemy, the scene will reload.

Collecting Coins

We want the player to achieve a certain goal, so let's add in some coins. Before we create the
scene, let's go and set up the scripting side of things over in the Player script.

The collect_coin function gets called when we run into a coin and increments our score by the
given parameter value.

Next, create a new scene and make the root node an Area2D. Give it a collision shape node as
a child with the shape being a circle. Then drag in the Coin sprite and center it.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
On the Area2D root node, create a new script called Coin. This is just going to detect when a
player has entered the collider, then call the collect_coin function.

We'll start with our only variable which is just the value of the coin.

The _process function is built into Godot and gets called every frame. We're going to use it in
order to rotate the coin over time.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Next, we need to select the Area2D node, in the Inspector go to the Node tab and double-click
on the body_entered signal to attach it to the script.

Back in the script, we can fill in the function to check if the entering body is the player. If so call
the collect_coin function and destroy the coin.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In the main scene, we can now drag in the coins to populate the level.

Tracking Camera
Right now our camera is static, so let's implement the ability for the camera to move
horizontally.

The camera is a new node we'll create as right now, the game is being rendered just based on
the position of the nodes and pixels, not the position of the camera. So in the MainScene, right
click on the parent node and create a new child node of type Camera2D.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
• Enable Current so that the camera will be active
• Move the camera to cover the level

Now we can create a new script on the camera node called CameraController.

First, we want to create a variable to store a reference to the player in order to track
them. get_node will search for a node based on the given path.

Then in the _process function (called every frame), we can set our X position to be the same as
the player's.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Now when we press play, you'll see that the camera follows along the player's X position.
Except that it does act a bit weird. Select the camera and in the Inspector, set the Drag
Margins to 0, 0, 0, 0. This means that there's no margin for us to move around freely, the
camera will always be tracking our X position.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Press play and you should see that it's all working fine now.

UI

Now that we have the game pretty much done, all we need to do is add in some sort of user
interface to show us our current score.

To begin, create a new scene with the root node being User Interface (control).

1. Rename the node to UI


2. Save the scene

You'll see that there is a colored rectangle. This is the bounds of the control node which is how
we build our UI elements.

This rect right now is covering the whole screen. Let's add in two new things.

• A coin icon
• A text box to show our current score

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Let's start with the icon. Create a new child node of type TextureRect. This is a control node
where we can show an image. In our file system, find the coin image and drag that into
the Texture field in the inspector.

Also set the Position to 20, 20.

Then for the text, we want to create a new child node of type Label. This is a control node
where we can display text.

1. Rename it to ScoreText
2. Set the Position to 90, 20
3. Set the Size to 100, 64

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Right now we can't see our text, and that's because we need a font. There is a default font but
it's quite limiting so we'll setup our own.

In the file system, open the Font folder, right click on the font file we have and select New
Resource... Find the DynamicFont resource and create that.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Select our new resource and in the inspector, drag the .ttf font file into the Font Data property to
assign it.

Under the Settings tab, set the Size to 40.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Select the ScoreText and under the Custom Fonts tab, drag in our new dynamic font resource.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We can now type in a placeholder number up in the Text property. To make the text centered
vertically, set the Valign property to Center.

Alright, we've got our UI setup. If we go to the MainScene we can drag the UI scene into the
node list and it should appear.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
If you press play though, you'll see that it doesn't follow the camera. To fix this, we need to
make this UI scene a child of a new node called CanvasLayer. This node defines what gets
rendered onto the screen as UI.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Scripting the UI

All that's left to do is connect the UI to a script. In the UI scene, create a new script called UI and
attach it to the parent node.

First, we need to create a variable which will get the score text node upon starting the game.

Then in our only function, we'll set the score text to whatever the parameter sends over.

Over in the Player script, let's create a variable to reference the UI node.

Then down in the collect_coin function, let's add a line of code to set the UI text.

If we press play now the score text will update when we collect a coin. Let's also set it up so that
the text is set initially when the game begins. In the UI script...

Now the text should be set to 0 when we start the game.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Conclusion

And that's it! If you hit Play on your Godot project now, everything should work. Congratulations
on completing your first game!

We now have a functional 2D platformer with a playable character, coins, UI, and a dangerous
enemy to obstruct our path. While you can certainly improve upon this framework to add even
more challenges, you've no doubt seen how the foundations of the framework taught here will
provide a useful starting point as you increase your game development skills further. The sky is
truly the limit, and you can expand as needed to create new game projects with these
fundamentals.

Either way, you've taken the first crucial step and created your first game! We wish you luck in
the future, and we can't wait to see the sorts of games you create!

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Create a First-Person Shooter in Godot - Part 1

Introduction

Welcome to the tutorial, and get ready to learn some in-demand skills for creating first-person
shooter games! Throughout this tutorial, we will work with the 3D aspects of the Godot game
engine to enhance your foundations when it comes to action game development. Additionally,
we'll be creating a first-person shooter from scratch, which will show you how to set up the FPS
player and camera, enemies, efficient shooting mechanics, health and ammo pickups, and
more.

Before we begin, it's important to know that a basic understanding of the Godot engine is
required. If this is your first time using Godot, I recommend you read through the How to Make
First Game with Godot chapter first. It will teach you how to install Godot, and the basics of the
engine and GDScript.

Project Files
For this project, we'll be using a handful of pre-made assets such as models and textures.
Some of these are custom-built, while others are from kenney.nl, a website for public domain
game assets.

The assets can be downloaded here.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
You can also download the completed Godot project via the same link!

Let's Begin
To begin, create a new Godot project. We have a few assets such as models and textures we'll
be using (download above), so let's drag the Font, Models and UI folders into the FileSystem.

Now that we have our assets, let's create our first scene by going to the scene panel and
selecting the 3D Scene option.

1. Rename the node to MainScene.


2. Save the main scene to the file system.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Building Our Environment
The first thing we're going to do is create our environment so we have somewhere for the player
to exist. In our Models/Environment folder, we have a Ground model. Drag that into the scene
window to create a new MeshInstance node. Set the position to 0.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
The MeshInstance node will render a model, but that's it. In our case, we want the player to be
able to collide with the ground, otherwise they'd sink through.

So to do this, we can select the ground node and in the scene panel, select Mesh > Create
Trimesh Static Body.

This will create two children nodes.

• StaticBody: static physics object which can detect collisions


• CollisionShape: defines the shape and dimensions of the object's collider

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
With this node, we can change the position, scale and rotation. Go ahead and create an
environment.

One thing you may notice, is that the node structure is starting to get quite long and convoluted
since we have a lot of models. To fix this, we can create a new node as a container and store
the models in as children.

Create a new node of type Node as a child of the MainScene. Drag the model nodes inside to
make them children. Now we can visually open or close the node container.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Another thing you might notice is that the scene is quite dark. To fix this, we can add in a
directional light which will act like a sun.

1. Create a new node of type DirectionalLight.


2. Enable shadows.
3. Set the Rotation Degrees to -60, 45, 0.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Creating the Player

Now we can create the player. Create a new scene with a root node of KinematicBody and
rename it to Player. We can then save the scene.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Let's add a CollisionShape node to detect collisions.

1. Set the Shape to Capsule


2. Set the Radius to 0.5
3. Set the Height to 1.5
4. Set the Translation to 0, 1.25, 0
5. Set the Rotation to 90, 0, 0

Next, add a new Camera node. This will allow us to see through the player's eyes.

1. Set the Environment to default_env.tres (found in file system)


2. Enable Current
3. Set the Translation to 0, 0.25, 0

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
For the gun, drag the shotgun.obj model into the scene and make it a child of Camera. To see
through the camera's perspective, select the camera and in the scene view toggle the Preview
option (top left of the window).

1. Rename the node to GunModel


2. Set the Translation to 0.2, -0.2, -0.7
3. Set the Rotation Degrees to 0, -180, 0
4. Set the Scale to 10, 10, 10

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
The gun also needs a muzzle so we know where to spawn the bullets. Create a new Spatial
node as a child of GunModel.

1. Enable Use Local Space (shortcut = T) so that the gizmos are orientated in local space
2. Rename the node to Muzzle
3. Position the node in front of the gun

Back in the MainScene, let's drag in the Player.tscn to create a new instance of the Player
scene.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
With this here, we can press the play button (make the MainScene the main scene) and you
should be looking through the player's eyes.

Finally, before we begin scripting, let's set up our input actions. Open the Project Settings
window (Project > Project Settings) and go to the Input Map tab. Create a new action for:

• move_forward (W key)
• move_backward (S key)
• move_left (A key)
• move_right (D key)
• jump (Space key)
• shoot (Left Mouse Button)

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Scripting the Player

Now that we have our player scene setup, let's get to scripting the player movement, jumping,
gravity, shooting, etc.

In the Player scene, create a new script on the Player node. By default, it should be called
Player.gd. We'll begin by filling in our variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
First, we can begin by setting up the ability to look around. The _Input function is built into
Godot and gets called whenever an input is detected (keyboard, mouse click and movement).
What we're doing here, is getting the direction and length that our mouse moved.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Next, we'll create the _process function. This is built into Godot and gets called every frame.
Here, we're going to use that mouseDelta vector and apply that to the camera and player
rotations.

We can now press Play and see that we look around when we move the mouse.

Next up is movement. This will all be done in the _physics_process function. It's built into the
KinematicBody node and gets called 60 times a second (good for physics).

First, we're going to check for our keyboard inputs.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Next, we need to get the forward and right direction of our player, so we know which way we're
facing and can apply that to our velocity.

We can then set our velocity, apply gravity and move the player!

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Finally, we're going to check for the jump action and change the Y velocity when that happens.

If we press the play button now, you'll be able to move, look around and jump. That's how to
create a basic first-person controller in Godot. Now let's get working on the rest of the game.

Creating the Bullet Scene


Before we start to script the ability to shoot, we need to create a scene for the bullet. Create a
new scene with a root node of Area. This is a node which can detect physics collisions.

1. Save the scene to the FileSystem.


2. Drag the ammo_pistol.obj model into the scene as a child of Bullet.
3. Set the model's Scale to 10, 10, 10. (not in image)
4. Set the model's Rotation Degrees to 90, 0, 0. (not in image)
5. Attach a new CollisionShape node.
6. Set the Shape to Capsule
7. Set the Radius to 0.03
8. Set the Height to 0.07

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
On the Bullet node, create a new script called Bullet.gd. We'll start of with the variables.

Inside of the _process function, we'll move the bullet forwards at a rate of speed units per
second.

Now it's time to detect if we're hitting something. Select the Bullet node and over in the
inspector, click on the Node tab to see all of the signals that the node emits. A signal is like an
event which can call a function when something happens. In our case, we want to connect

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
the body_entered signal to our script. This gets called when the collider enters another node's
collider.

Double click on that signal and hit enter. A new function should have appeared in the bullet
script.

We're going to check if the body we hit has a function called take_damage. We haven't created
this yet, but we will do it soon. If so, deal the damage and destroy the bullet by calling the
destroy function. This is a custom function we're going to create right after this one.

The destroy function will simply destroy the bullet instance.

We now have a bullet which will move forward and damage whatever it hits (yet to be added).
Let's also add in something so that the bullet can't travel forever if it misses its target.

In the Bullet scene, create a new node of type Timer. This node will count up and when it hits a
certain amount, we'll call the destroy function.

1. Set the Wait Time to 5


2. Enable Autostart

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Over in the node tab, let's connect the timeout signal to our script. This gets called once the
wait time has been reached. Double click on the timeout signal and a window will pop-up.
Change the Receiver Method to destroy. This will connect the signal to our existing function.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Shooting Bullets

Now that we have our bullet, let's implement the ability to shoot it. Over in the Player script, let's
create some new variables to keep track of our components.

Down in the _process function, let's check for when the shoot action is pressed.

The shoot function will spawn a new instance of the bullet scene and position it.

If you want the mouse to be locked when we're playing the game, we can do so in the _ready
function which gets called when the node is initialized.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We can now press play and test it out.

Creating the Enemy


Create a new scene with a root node of KinematicBody.

1. Rename it to Enemy.
2. Save the scene.
3. Attach a new node of type MeshInstance.
4. Rename it to Model.
5. Set the Mesh to capsule.
6. Set the Radius to 0.5.
7. Set the Mid Height to 1.5.
8. Rotate and re-position the mesh so that it is orientated like below.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In the MeshInstance node, you'll see a Material property which is empty. Select it and
click New SpatialMaterial.

1. Set the Albedo Color to red.

Let's then attach a new CollisionShape node and make it match the mesh.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Finally, we'll attach a Timer node to the enemy. This is going to be used in order to attack the
player.

Continued Part 2

And that's where we'll conclude for the time being!

At this point, we've learned several essential foundations for setting up a first-person shooter in
Godot. First, we established our arena where all the action will take place. We then created a
player, along with a camera, which is scripted to follow the mouse movements as we move the
player around. To this player, we also added a gun model that can shoot bullets - which we
also created with performance in mind. Lastly, we set up our enemy model and learned a bit
about materials in the process.

Of course, there is still more needed to complete our FPS! In Part 2 of this tutorial, we will finish
up building our Godot FPS by implementing the enemies' movement, setting up ammo and
health pickups, and polishing everything off with a nifty UI! We hope to see you there.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Create a First-Person Shooter in Godot - Part 2

Introduction

Welcome back, and I hope you're ready to finish creating our Godot FPS tutorial.

In Part 1, we set up our arena, our player character, our FPS camera, our gun, our bullets, and
even our red enemies. However, while we certainly implemented the shooting mechanics, our
enemies can't yet damage players, get damaged themselves, or move. In addition, we have no
pickups to speak of, let alone a UI to give the player essential health and ammo information. As
such, in this tutorial, we will be jumping into setting those up.

By the end, you will have not only learned a lot about 3D game development, but also have a
nifty FPS to add to your portfolio!

Project Files

For this project, we'll be using a handful of pre-made assets such as models and textures.
Some of these are custom-built, while others are from kenney.nl, a website for public domain
game assets.

The assets and source code can be downloaded here.

Scripting the Enemy


Create a new script on the Enemy node. Let's begin with the variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In the _ready function, we'll set up the timer to timeout every attackRate seconds.

If we select the Timer node, we can connect the timeout signal to the script. This will create
the _on_Timer_timeout function. We'll be working on this later on.

In the _physics_process function, we'll move towards the player.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
The take_damage function gets called when we get damaged by the player's bullets.

The die function gets called when our health reaches 0. The add_score function for the player
will be added soon.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
The last function to add is the Attack function. We'll be creating the player's take_damage
function soon.

Finally in the _on_Timer_timeout function, we can check the distance to the player and try to
attack them.

Player Functions

In the Player script, we're going to add in a number of functions which we need right now and in
the future. The die function will be filled in later once we have our UI setup.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Now we can go to the MainScene and drag the Enemy scene into the scene window to create
a new instance of the enemy. Press play and test it out.

Pickups
For our pickups, we're going to create one template scene which the health and ammo pack will
inherit from. Create a new scene with a root node of Area.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
2. Rename it to Pickup.
3. Save the scene.
4. Attach a child node of type CollisionShape.
5. Set the Shape to Sphere.
6. Set the Radius to 0.5.

Next, create a script on the Pickup node. First, we'll create an enumerator which is a custom
data type that contains different options.

Then for our variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In the _process function, we're going to make the pickup bob up and down.

Select the Pickup node and connect the body_entered node to the script.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
The pickup function will give the player the appropriate stat increase.

Now that we've finished the script, let's go back to the scene and you'll see that the Pickup
node now has two exposed variables.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We're now going to create two inherited scenes from this original Pickup one. Go to Scene >
New Inherited Scene... and a window will pop up asking to select a base scene. Select
the Pickup.tscn and a new scene should be created for you. You'll see that there's already the
area and collider nodes there since they are a parent. This means any changes to the original
Pickup scene, those changes will also be applied to the inherited scenes.

All we need to do here is...

• Rename the area node to Pickup_Health


• Set the pickup type to Health
• Drag in the health pack model

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
We also want to do the same for the ammo pickup.

Back in the MainScene, we can drag in the Enemy, Pickup_Health and Pickup_Ammo scenes
and place them around.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
UI

Now it's time to create the UI which will display our health, ammo, and score. Create a new
scene with the root node being User Interface (control node).

• Rename the node to UI


• Create a new child node of type TextureProgress
• Enable Nine Patch Stretch
• Rename it to HealthBar
• Move the health bar to the bottom left of the screen and re-size it
• Drag the 4 anchor points (green pins) to the bottom left of the screen
• Set the Under and Progress textures to the UI_Square.png image
• Set the Tints as seen in the image.

For the text, we need to create a new dynamic font resource. In the file system, find
the Ubuntu-Regular.ttf file - right click it and select New Resource...

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
• Search for and create a DynamicFont
• In the inspector, set the Font Data to the ubuntu font file
• Set the Size to 30

Now we can create the text elements. Create a new Label node and call it AmmoText.

• Resize and position it like in the image below


• Set the Custom Font to the new dynamic font file
• Move the anchor points down to the bottom left

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
With the node selected, press Ctrl + D to duplicate the text.

• Rename it to ScoreText
• Move it above the ammo text

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Scripting the UI

Now that we have our UI elements, let's create a new script attached to the UI node called UI.

First, we can create our variables.

Then we're going to have three functions which will each update their respective UI element.

So we got the functions to update the UI nodes. Let's now connect this to the Player script.
We'll start by creating a variable to reference the UI node.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Then in the _ready function, we can initialize the UI.

We want to update the ammo text in both the shoot and add_ammo functions.

We want to update the health bar in both the take_damage and add_health functions.

We want to update the score text in the add_score function.

And now we can go back to the MainScene and create a new node called CanvasLayer.
Whatever is a child of this, gets rendered to the screen so let's now drag in the UI scene as a
child of this node.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Now we can press play and see that the UI is on-screen and updates when we take damage,
collect pickups, and kill enemies.

Conclusion
Congratulations on completing the tutorial!

If you've been following along, you should now have a complete Godot FPS game at your
fingertips. Players will be able to fire on enemies, gather pickups for ammo and health, and
even potentially be defeated by our menacing red capsules! Not only that, but you also have
boosted your own knowledge in how Godot's 3D engine works and how you can utilize 3D's
unique features in a number of ways. Of course, from here, you can expand upon the FPS
game we created in any way you please - adding in new systems, models, sound, etc.

Thanks for following along, and I hope to see you in the next Godot tutorial.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Build a 2D RPG in Godot - Part 1

Introduction

Welcome, everyone! Even after all the years games have existed, RPGs are still one of the
most popular genres that aspiring developers want to learn how to make. However, given how
feature-rich RPGs can be, it can be hard to know where to start. Thankfully, though, with Godot
and some programming knowledge at hand, you will have all the tools you need to start on your
very own dream RPG!

In this RPG tutorial series, we'll be creating a 2D RPG inside of the Godot game engine
together. It will feature a top-down player controller, enemies, combat, loot, and even a leveling
system. For Part 1, though, we will be covering a number of new systems inside of Godot as
we start to build our RPG, including tilemaps, sprite animations, and raycasting. This course will
require a basic understanding of the Godot game engine and the GDScript scripting language. If
you're new to Godot, you can view our introductory tutorial here.

Otherwise, though, we hope you strap in and are ready to learn how to make 2D RPGs with
Godot.

If you'd like to jump straight in making enemies and loot, check out Part 2 instead!

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Project Files
For this project, we'll be needing some assets such as sprites and a font. These will be sourced
from kenney.nl and Google Fonts. You can, of course, choose to use your own assets, but for
this course we'll be using these.

The assets and source code can be downloaded here.

Setting Up The Project


To begin, let's create a new Godot project. Inside of the editor, let's then import the assets we'll
be needing.

Once we have the assets, let's create our first scene by selecting 2D Scene in the scene panel.
Rename this node to MainScene, then save the scene to the file system.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Creating the Player
Now that we have the main scene, let's create a new scene (Scene > New Scene) of
type KinematicBody2D.

• Rename the node to Player


• Save the scene to the file system

As a child of this node, we want to...

• Drag in the player_s_0.png sprite in to create a new sprite node


• Set the position to 0, 0
• Create a child node of type CollisionShape2D
• Set the Shape to Capsule
• Resize it to fit the sprite

Right now, we're using just a sprite node to allow us to see the player. Later on, we'll be
replacing this with an AnimatedSprite node.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Finally, for our player, let's create a RayCast2D node. You'll see that in the scene view, an
arrow will appear to come out of the player. This is a raycast, which is a concept in game
development that is used in many different cases. You give a raycast an origin position and a
direction. Then it will shoot a point from that origin in the given direction. If that point ever hits a
collider it will return that object and some other information.

In our case, the raycast has a max distance and this will be used to detect interactable objects
and enemies.

• Make sure to enable Enabled

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Scripting the Player Movement

Now that we have the player scene, let's create a new script attached to the kinematic body
node called Player. We can start with our variables.

Later on, we'll be adding in a couple more variables, but for most of our systems, this will do.
Now, in order to move the player around, we'll need to know which buttons do what. Go to the
Project Settings window (Project > Project Settings...) and click on the Input Map tab. Here,
we want to create 5 new actions and assign a keyboard key to each of them.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
• move_left - left arrow key
• move_right - right arrow key
• move_up - up arrow key
• move_down - down arrow key
• interact – space

Back in the script, we can create the _physics_process function. A built-in function which runs
at 60 FPS (good for physics). What we want to do here, is a few things.

1. Reset vel (vector containing our velocity)


2. Detect the 4 direction keys to change the velocity and facing direction (used for
animations later)
3. Normalize the velocity vector to prevent faster diagonal movement
4. Move the player based on the velocity using the KinematicBody2D node function

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Let's hop over to the MainScene and test the movement out. In the file system, drag the player
scene into the scene window to create a new instance of it. Then click the Play button, choose
the main scene to be the base scene and see if the movement works.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
Animating the Player

You'll see that we can move around, but it's pretty bland. Let's now implement sprite animations.
In the Player scene, delete the Sprite node and replace it with an AnimatedSprite node.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
In the inspector, create a new sprite frames resource. The SpriteFrames window will then pop-
up.

In the animations list there will be an animation called default. Double click on it and rename it
to IdleDown. For this animation, we're going to drag in the player_s_0 sprite.

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved
From here, we can create the rest of the animations. These will be:

• IdleDown (already created)


• IdleUp
• IdleLeft
• IdleRight
• MoveDown
• MoveUp
• MoveLeft
• MoveRight

Here's what the MoveDown animation will look like. Click on the New Animation button to
create a new animation.

Go through now and create all of the idle and move animations. Once that's done, select
the AnimatedSprite node and...

This book is brought to you by Zenva - Enroll in our Godot Game Development Mini-Degree to master 2D
and 3D game development with the free, open-source Godot game engine
© Zenva Pty Ltd 2020. All rights reserved

You might also like