Important Note – Please Read
This tutorial series will show you how to make a single player FPS game.
Throughout the course of this tutorial series, we will cover how:
- To make a first person character that can move, sprint, and jump.
- To make a simple animation state machine for handling animation transitions.
- To add three weapons to the first person character, each using a different way to handle bullet collisions:
- A knife (using an Area)
- A pistol (Bullet scenes)
- A rifle (using a Raycast)
- To add two different types of grenades to the first person character:
- A normal grenade
- A sticky grenade
- To add the ability to grab and throw RigidBody nodes
- To add joypad input for the player
- To add ammo and reloading for all weapons that consume ammo.
- To add ammo and health pick ups
- In two sizes: big and small
- To add an automatic turret
- That can fire using bullet objects or a Raycast
- To add targets that break when they’ve taken enough damage
- To add sounds that play when the guns fire.
- To add a simple main menu:
- With an options menu for changing how the game runs
- With a level select screen
- To add a universal pause menu we can access anywhere
You can find the start assets for this tutorial here:
The provided starter assets contain an animated 3D model, a bunch of 3D models for making levels, and a few scenes already configured for this tutorial.
All assets provided (unless otherwise noted) were originally created by TwistedTwigleg, with changes/additions by the Godot community. All original assets provided for this tutorial are released under the
Feel free to use these assets however you want! All original assets belong to the Godot community, with the other assets belonging to those listed below:
In this part we will be making a first person player that can move around the environment.
By the end of this part, you will have a working first-person character who can move around the game environment, sprint, look around with a mouse based first person camera, jump into the air, and turn a flash light on and off.
Getting everything ready
Launch Godot and open up the project included in the starter assets.
First, open the project settings and go to the “Input Map” tab. You’ll find several actions have already been defined. We will be using these actions for our player. Feel free to change the keys bound to these actions if you want.
Let’s take a second to see what we have in the starter assets.
Included in the starter assets are several scenes. For example, in
res:// we have 14 scenes, most of which we will be visiting as we go through this tutorial series.
For now let’s open up
Making the FPS movement logic
Once you have
Player.tscn open, let’s take a quick look at how it is set up
First, notice how the player’s collision shapes are set up. Using a vertical pointing capsule as the collision shape for the player is fairly common in most first person games.
We are adding a small square to the ‘feet’ of the player so the player does not feel like they are balancing on a single point.
We do want the ‘feet’ slightly higher than the bottom of the capsule so we can roll over slight edges. Where to place the ‘feet’ is dependent on your levels and how you want your player to feel.
Another thing to notice is how many nodes are children of
Rotation_Helper. This is because
Rotation_Helper contains all the nodes we want to rotate on the
X axis (up and down). The reason behind this is so we can rotate
Player on the
Y axis, and
Rotation_helper on the
Attach a new script to the
Player node and call it
Let’s program our player by adding the ability to move around, look around with the mouse, and jump. Add the following code to
const GRAVITY = -24.8
var vel = Vector3()
const MAX_SPEED = 20
const JUMP_SPEED = 18
const ACCEL = 4.5
var dir = Vector3()
const DEACCEL= 16
const MAX_SLOPE_ANGLE = 40
var MOUSE_SENSITIVITY = 0.05
camera = $Rotation_Helper/Camera
rotation_helper = $Rotation_Helper
dir = Vector3()
var cam_xform = camera.get_global_transform()
var input_movement_vector = Vector2()
input_movement_vector.y += 1
input_movement_vector.y -= 1
input_movement_vector.x -= 1
input_movement_vector.x += 1
input_movement_vector = input_movement_vector.normalized()
# Basis vectors are already normalized.
dir += -cam_xform.basis.z * input_movement_vector.y
dir += cam_xform.basis.x * input_movement_vector.x
vel.y = JUMP_SPEED
# Capturing/Freeing the cursor
if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
dir.y = 0
dir = dir.normalized()
vel.y += delta * GRAVITY
var hvel = vel
hvel.y = 0
var target = dir
target *= MAX_SPEED
if dir.dot(hvel) > 0:
accel = ACCEL
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel * delta)
vel.x = hvel.x
vel.z = hvel.z
vel = move_and_slide(vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY))
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
var camera_rot = rotation_helper.rotation_degrees
camera_rot.x = clamp(camera_rot.x, -70, 70)
rotation_helper.rotation_degrees = camera_rot
This is a lot of code, so let’s break it down function by function:
First, we define some class variables to dictate how our player will move about the world.
Let’s go through each of the class variables:
GRAVITY: How strong gravity pulls us down.
vel: Our KinematicBody‘s velocity.
MAX_SPEED: The fastest speed we can reach. Once we hit this speed, we will not go any faster.
JUMP_SPEED: How high we can jump.
ACCEL: How quickly we accelerate. The higher the value, the sooner we get to max speed.
DEACCEL: How quickly we are going to decelerate. The higher the value, the sooner we will come to a complete stop.
MAX_SLOPE_ANGLE: The steepest angle our KinematicBody will consider as a ‘floor’.
camera: The Camera node.
rotation_helper: A Spatial node holding everything we want to rotate on the X axis (up and down).
MOUSE_SENSITIVITY: How sensitive the mouse is. I find a value of
0.05works well for my mouse, but you may need to change it based on how sensitive your mouse is.
You can tweak many of these variables to get different results. For example, by lowering
GRAVITY and/or increasing
JUMP_SPEED you can get a more ‘floaty’ feeling character. Feel free to experiment!
Now let’s look at the
First we get the
rotation_helper nodes and store them into their variables.
Then we need to set the mouse mode to captured, so the mouse cannot leave the game window.
This will hide the mouse and keep it at the center of the screen. We do this for two reasons: The first reason being we do not want the player to see their mouse cursor as they play.
The second reason is because we do not want the cursor to leave the game window. If the cursor leaves the game window there could be instances where the player clicks outside the window, and then the game would lose focus. To assure neither of these issues happens, we capture the mouse cursor.
Next let’s take a look at
All we’re doing in
_physics_process is calling two functions:
process_input will be where we store all the code relating to player input. We want to call it first, before anything else, so we have fresh player input to work with.
process_movement is where we’ll send all the data necessary to the KinematicBody so it can move through the game world.
Let’s look at
First we set
dir to an empty Vector3.
dir will be used for storing the direction the player intends to move towards. Because we do not want the player’s previous input to effect the player beyond a single
process_movement call, we reset
Next we get the camera’s global transform and store it as well, into the
The reason we need the camera’s global transform is so we can use its directional vectors. Many have found directional vectors confusing, so let’s take a second to explain how they work:
World space can be defined as: The space in which all objects are placed in, relative to a constant origin point. Every object, no matter if it is 2D or 3D, has a position in world space.
To put it another way: world space is the space in a universe where every object’s position, rotation, and scale can be measured by a single, known, fixed point called the origin.
In Godot, the origin is at position
(0, 0, 0) with a rotation of
(0, 0, 0) and a scale of
(1, 1, 1).
If you want to move using the world space directional vectors, you’d do something like this:
node.translate(Vector3(0, 0, 1))
node.translate(Vector3(0, 0, -1))
node.translate(Vector3(1, 0, 0))
node.translate(Vector3(-1, 0, 0))
Here is what world space looks like in 2D:
And here is what it looks for for 3D:
Notice how in both examples, the rotation of the node does not change the directional arrows. This is because world space is a constant. No matter how you translate, rotate, or scale an object, world space will always point in the same direction.
Local space is different, because it takes the rotation of the object into account.
Local space can be defined as follows: The space in which an object’s position is the origin of the universe. Because the position of the origin can be at
N many locations, the values derived from local space change with the position of the origin.
Each Basis has three vectors:
Z. Each of those vectors point towards each of the local space vectors coming from that object.
To use the Spatial node’s local directional vectors, we use this code:
Here is what local space looks like in 2D:
And here is what it looks like for 3D:
Here is what the Spatial gizmo shows when you are using local space mode. Notice how the arrows follow the rotation of the object on the left, which looks exactly the same as the 3D example for local space.
Local vectors are confusing even for more experienced game developers, so do not worry if this all doesn’t make a lot of sense. The key thing to remember about local vectors is that we are using local coordinates to get direction from the object’s point of view, as opposed to using world vectors, which give direction from the world’s point of view.
Okay, back to
Next we make a new variable called
input_movement_vector and assign it to an empty Vector2. We will use this to make a virtual axis of sorts, to map the player’s input to movement.
Based on which directional movement action is pressed, we add to or subtract from
After we’ve checked each of the directional movement actions, we normalize
input_movement_vector. This makes it where
input_movement_vector‘s values are within a
1 radius unit circle.
Next we add the camera’s local
Z vector times
dir. This is so when the player presses forward or backwards, we add the camera’s local
Z axis so the player moves forward or backwards in relation to the camera.
We do the same thing for the camera’s local
X vector, and instead of using
input_movement_vector.y we instead use
input_movement_vector.x. This makes it where the player moves left/right in relation to the camera when the player presses left/right.
Next we check if the player is on the floor using KinematicBody‘s
is_on_floor function. If it is, then we check to see if the “movement_jump” action has just been pressed. If it has, then we set the player’s
Y velocity to
Because we’re setting the Y velocity, the player will jump into the air.
Then we check for the
ui_cancel action. This is so we can free/capture the mouse cursor when the
escape button is pressed. We do this because otherwise we’d have no way to free the cursor, meaning it would be stuck until you terminate the runtime.
To free/capture the cursor, we check to see if the mouse is visible (freed) or not. If it is, we capture it, and if it’s not, we make it visible (free it).
That’s all we’re doing right now for
process_input. We’ll come back several times to this function as we add more complexities to our player.
Now let’s look at
First we ensure that
dir does not have any movement on the
Y axis by setting its
Y value to zero.
Next we normalize
dir to ensure we’re within a
1 radius unit circle. This makes it where we’re moving at a constant speed regardless of whether the player is moving straight or diagonally. If we did not normalize, the player would move faster on the diagonal than when going straight.
Next we add gravity to the player by adding
GRAVITY * delta to the player’s
After that we assign the player’s velocity to a new variable (called
hvel) and remove any movement on the
Next we set a new variable (
target) to the player’s direction vector. Then we multiply that by the player’s max speed so we know how far the player will move in the direction provided by
After that we make a new variable for acceleration, named
We then take the dot product of
hvel to see if the player is moving according to
hvel does not have any
Y velocity, meaning we are only checking if the player is moving forwards, backwards, left, or right.
If the player is moving according to
hvel, then we set
accel to the
ACCEL constant so the player will accelerate, otherwise we set
accel to our
DEACCEL constant so the player will decelerate.
Then we interpolate the horizontal velocity, set the player’s
Z velocity to the interpolated horizontal velocity, and call
move_and_slide to let the KinematicBody handle moving the player through the physics world.
The final function we have is the
_input function, and thankfully it’s fairly short:
First we make sure that the event we are dealing with is an InputEventMouseMotion event. We also want to check if the cursor is captured, as we do not want to rotate if it is not.
If the event is indeed a mouse motion event and the cursor is captured, we rotate based on the relative mouse motion provided by InputEventMouseMotion.
First we rotate the
rotation_helper node on the
X axis, using the relative mouse motion’s
Y value, provided by InputEventMouseMotion.
Then we rotate the entire KinematicBody on the
Y axis by the relative mouse motion’s
Finally, we clamp the
X rotation to be between
70 degrees so the player cannot rotate themselves upside down.
To test the code, open up the scene named
Testing_Area.tscn, if it’s not already opened up. We will be using this scene as we go through the next few tutorial parts, so be sure to keep it open in one of your scene tabs.
Go ahead and test your code either by pressing F6 with
Testing_Area.tscn as the open tab, by pressing the play button in the top right corner, or by pressing F5. You should now be able to walk around, jump in the air, and look around using the mouse.
Giving the player a flash light and the option to sprint
Before we get to making the weapons work, there are a couple more things we should add.
Many FPS games have an option to sprint and a flashlight. We can easily add these to our player, so let’s do that!
First we need a few more class variables in our player script:
const MAX_SPRINT_SPEED = 30
const SPRINT_ACCEL = 18
var is_sprinting = false
All the sprinting variables work exactly the same as the non sprinting variables with similar names.
is_sprinting is a boolean to track whether the player is currently sprinting, and
flashlight is a variable we will be using to hold the player’s flash light node.
Now we need to add a few lines of code, starting in
_ready. Add the following to
flashlight = $Rotation_Helper/Flashlight
This gets the
Flashlight node and assigns it to the
Now we need to change some of the code in
process_input. Add the following somewhere in
is_sprinting = true
is_sprinting = false
# Turning the flashlight on/off
Let’s go over the additions:
true when the player is holding down the
movement_sprint action, and
false when the
movement_sprint action is released. In
process_movement we’ll add the code that makes the player faster when they sprint. Here in
process_input we are just going to change the
We do something similar to freeing/capturing the cursor for handling the flashlight. We first check to see if the
flashlight action was just pressed. If it was, we then check to see if
flashlight is visible in the scene tree. If it is, then we hide it, and if it’s not, we show it.
Now we need to change a couple things in
process_movement. First, replace
target *= MAX_SPEED with the following:
target *= MAX_SPRINT_SPEED
target *= MAX_SPEED
Now instead of always multiplying
MAX_SPEED, we first check to see if the player is sprinting or not. If the player is sprinting, we instead multiply
Now all that’s left is to change the acceleration when sprinting. Change
accel = ACCEL to the following:
accel = SPRINT_ACCEL
accel = ACCEL
Now, when the player is sprinting, we’ll use
SPRINT_ACCEL instead of
ACCEL, which will accelerate the player faster.
You should now be able to sprint if you press Shift, and can toggle the flash light on and off by pressing F!
Go try it out! You can change the sprint-related class variables to make the player faster or slower when sprinting!
Whew! That was a lot of work. Now you have a fully working first person character!
In Part 2 we will add some guns to our player character.