Part 1

Tutorial introduction

../../../_images/FinishedTutorialPicture.png

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)
  • 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

Note

While this tutorial can be completed by beginners, it is highly advised to complete Your first game, if you are new to Godot and/or game development before going through this tutorial series.

Remember: Making 3D games is much harder than making 2D games. If you do not know how to make 2D games, you will likely struggle making 3D games.

This tutorial assumes you have experience working with the Godot editor, basic programming experience in GDScript, and basic experience in game development.

You can find the start assets for this tutorial here: Godot_FPS_Starter.zip

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 MIT license.

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:

Note

The skybox is created by StumpyStrust on OpenGameArt. The skybox used is licensed under CC0.

The font used is Titillium-Regular, and is licensed under the SIL Open Font License, Version 1.1.

Tip

You can find the finished project for each part at the bottom of each part’s page

Part overview

In this part we will be making a first person player that can move around the environment.

../../../_images/PartOneFinished.png

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.

Note

While these assets are not necessarily required to use the scripts provided in this tutorial, they will make the tutorial much easier to follow, as there are several pre-setup scenes we will be using throughout the tutorial series.

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 Player.tscn.

Note

There are a bunch of scenes and a few textures in the Assets folder. You can look at these if you want, but we will not be exploring through Assets in this tutorial series. Assets contains all the models used for each of the levels, as well as some textures and materials.

Making the FPS movement logic

Once you have Player.tscn open, let’s take a quick look at how it is set up:

../../../_images/PlayerSceneTree.png

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.

Note

Many times the player will notice the collision shape being circular when they walk to an edge and slide off. We are adding the small square at the bottom of the capsule to reduce sliding on, and around, edges.

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 X axis.

Note

Had we not used Rotation_helper, we would’ve likely had cases of rotating on both the X and Y axes simultaneously, potentially further degenerating into a state of rotation on all three axes in some cases.

See using transforms for more information


Attach a new script to the Player node and call it Player.gd.

Let’s program our player by adding the ability to move around, look around with the mouse, and jump. Add the following code to Player.gd:

GDScript

C#

  1. extends KinematicBody
  2. const GRAVITY = -24.8
  3. var vel = Vector3()
  4. const MAX_SPEED = 20
  5. const JUMP_SPEED = 18
  6. const ACCEL = 4.5
  7. var dir = Vector3()
  8. const DEACCEL= 16
  9. const MAX_SLOPE_ANGLE = 40
  10. var camera
  11. var rotation_helper
  12. var MOUSE_SENSITIVITY = 0.05
  13. func _ready():
  14. camera = $Rotation_Helper/Camera
  15. rotation_helper = $Rotation_Helper
  16. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  17. func _physics_process(delta):
  18. process_input(delta)
  19. process_movement(delta)
  20. func process_input(delta):
  21. # ----------------------------------
  22. # Walking
  23. dir = Vector3()
  24. var cam_xform = camera.get_global_transform()
  25. var input_movement_vector = Vector2()
  26. if Input.is_action_pressed("movement_forward"):
  27. input_movement_vector.y += 1
  28. if Input.is_action_pressed("movement_backward"):
  29. input_movement_vector.y -= 1
  30. if Input.is_action_pressed("movement_left"):
  31. input_movement_vector.x -= 1
  32. if Input.is_action_pressed("movement_right"):
  33. input_movement_vector.x += 1
  34. input_movement_vector = input_movement_vector.normalized()
  35. # Basis vectors are already normalized.
  36. dir += -cam_xform.basis.z * input_movement_vector.y
  37. dir += cam_xform.basis.x * input_movement_vector.x
  38. # ----------------------------------
  39. # ----------------------------------
  40. # Jumping
  41. if is_on_floor():
  42. if Input.is_action_just_pressed("movement_jump"):
  43. vel.y = JUMP_SPEED
  44. # ----------------------------------
  45. # ----------------------------------
  46. # Capturing/Freeing the cursor
  47. if Input.is_action_just_pressed("ui_cancel"):
  48. if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
  49. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  50. else:
  51. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  52. # ----------------------------------
  53. func process_movement(delta):
  54. dir.y = 0
  55. dir = dir.normalized()
  56. vel.y += delta * GRAVITY
  57. var hvel = vel
  58. hvel.y = 0
  59. var target = dir
  60. target *= MAX_SPEED
  61. var accel
  62. if dir.dot(hvel) > 0:
  63. accel = ACCEL
  64. else:
  65. accel = DEACCEL
  66. hvel = hvel.linear_interpolate(target, accel * delta)
  67. vel.x = hvel.x
  68. vel.z = hvel.z
  69. vel = move_and_slide(vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
  70. func _input(event):
  71. if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  72. rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY))
  73. self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
  74. var camera_rot = rotation_helper.rotation_degrees
  75. camera_rot.x = clamp(camera_rot.x, -70, 70)
  76. rotation_helper.rotation_degrees = camera_rot
  1. using Godot;
  2. using System;
  3. public class Player : KinematicBody
  4. {
  5. [Export]
  6. public float Gravity = -24.8f;
  7. [Export]
  8. public float MaxSpeed = 20.0f;
  9. [Export]
  10. public float JumpSpeed = 18.0f;
  11. [Export]
  12. public float Accel = 4.5f;
  13. [Export]
  14. public float Deaccel = 16.0f;
  15. [Export]
  16. public float MaxSlopeAngle = 40.0f;
  17. [Export]
  18. public float MouseSensitivity = 0.05f;
  19. private Vector3 _vel = new Vector3();
  20. private Vector3 _dir = new Vector3();
  21. private Camera _camera;
  22. private Spatial _rotationHelper;
  23. // Called when the node enters the scene tree for the first time.
  24. public override void _Ready()
  25. {
  26. _camera = GetNode<Camera>("Rotation_Helper/Camera");
  27. _rotationHelper = GetNode<Spatial>("Rotation_Helper");
  28. Input.SetMouseMode(Input.MouseMode.Captured);
  29. }
  30. public override void _PhysicsProcess(float delta)
  31. {
  32. ProcessInput(delta);
  33. ProcessMovement(delta);
  34. }
  35. private void ProcessInput(float delta)
  36. {
  37. // -------------------------------------------------------------------
  38. // Walking
  39. _dir = new Vector3();
  40. Transform camXform = _camera.GlobalTransform;
  41. Vector2 inputMovementVector = new Vector2();
  42. if (Input.IsActionPressed("movement_forward"))
  43. inputMovementVector.y += 1;
  44. if (Input.IsActionPressed("movement_backward"))
  45. inputMovementVector.y -= 1;
  46. if (Input.IsActionPressed("movement_left"))
  47. inputMovementVector.x -= 1;
  48. if (Input.IsActionPressed("movement_right"))
  49. inputMovementVector.x += 1;
  50. inputMovementVector = inputMovementVector.Normalized();
  51. // Basis vectors are already normalized.
  52. _dir += -camXform.basis.z * inputMovementVector.y;
  53. _dir += camXform.basis.x * inputMovementVector.x;
  54. // -------------------------------------------------------------------
  55. // -------------------------------------------------------------------
  56. // Jumping
  57. if (IsOnFloor())
  58. {
  59. if (Input.IsActionJustPressed("movement_jump"))
  60. _vel.y = JumpSpeed;
  61. }
  62. // -------------------------------------------------------------------
  63. // -------------------------------------------------------------------
  64. // Capturing/Freeing the cursor
  65. if (Input.IsActionJustPressed("ui_cancel"))
  66. {
  67. if (Input.GetMouseMode() == Input.MouseMode.Visible)
  68. Input.SetMouseMode(Input.MouseMode.Captured);
  69. else
  70. Input.SetMouseMode(Input.MouseMode.Visible);
  71. }
  72. // -------------------------------------------------------------------
  73. }
  74. private void ProcessMovement(float delta)
  75. {
  76. _dir.y = 0;
  77. _dir = _dir.Normalized();
  78. _vel.y += delta * Gravity;
  79. Vector3 hvel = _vel;
  80. hvel.y = 0;
  81. Vector3 target = _dir;
  82. target *= MaxSpeed;
  83. float accel;
  84. if (_dir.Dot(hvel) > 0)
  85. accel = Accel;
  86. else
  87. accel = Deaccel;
  88. hvel = hvel.LinearInterpolate(target, accel * delta);
  89. _vel.x = hvel.x;
  90. _vel.z = hvel.z;
  91. _vel = MoveAndSlide(_vel, new Vector3(0, 1, 0), false, 4, Mathf.Deg2Rad(MaxSlopeAngle));
  92. }
  93. public override void _Input(InputEvent @event)
  94. {
  95. if (@event is InputEventMouseMotion && Input.GetMouseMode() == Input.MouseMode.Captured)
  96. {
  97. InputEventMouseMotion mouseEvent = @event as InputEventMouseMotion;
  98. _rotationHelper.RotateX(Mathf.Deg2Rad(mouseEvent.Relative.y * MouseSensitivity));
  99. RotateY(Mathf.Deg2Rad(-mouseEvent.Relative.x * MouseSensitivity));
  100. Vector3 cameraRot = _rotationHelper.RotationDegrees;
  101. cameraRot.x = Mathf.Clamp(cameraRot.x, -70, 70);
  102. _rotationHelper.RotationDegrees = cameraRot;
  103. }
  104. }
  105. }

This is a lot of code, so let’s break it down function by function:

Tip

While copy and pasting code is ill advised, as you can learn a lot from manually typing the code in, you can copy and paste the code from this page directly into the script editor.

If you do this, all the code copied will be using spaces instead of tabs.

To convert the spaces to tabs in the script editor, click the “edit” menu and select “Convert Indent To Tabs”. This will convert all the spaces into tabs. You can select “Convert Indent To Spaces” to convert tabs back into spaces.


First, we define some class variables to dictate how our player will move about the world.

Note

Throughout this tutorial, variables defined outside functions will be referred to as “class variables”. This is because we can access any of these variables from any place in the script.

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.05 works 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!

Note

You may have noticed that MOUSE_SENSITIVITY is written in all caps like the other constants, but MOUSE_SENSITIVITY is not a constant.

The reason behind this is we want to treat it like a constant variable (a variable that cannot change) throughout our script, but we want to be able to change the value later when we add customizable settings. So, in an effort to remind ourselves to treat it like a constant, it’s named in all caps.


Now let’s look at the _ready function:

First we get the camera and 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.

Note

See Input documentation for the various mouse modes. We will only be using MOUSE_MODE_CAPTURED and MOUSE_MODE_VISIBLE in this tutorial series.


Next let’s take a look at _physics_process:

All we’re doing in _physics_process is calling two functions: process_input and process_movement.

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 process_input next:

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 dir.

Next we get the camera’s global transform and store it as well, into the cam_xform variable.

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).

Note

When you open up the Godot editor and select a Spatial based node, a gizmo pops up. Each of the arrows points using world space directions by default.

If you want to move using the world space directional vectors, you’d do something like this:

GDScript

C#

  1. if Input.is_action_pressed("movement_forward"):
  2. node.translate(Vector3(0, 0, 1))
  3. if Input.is_action_pressed("movement_backward"):
  4. node.translate(Vector3(0, 0, -1))
  5. if Input.is_action_pressed("movement_left"):
  6. node.translate(Vector3(1, 0, 0))
  7. if Input.is_action_pressed("movement_right"):
  8. node.translate(Vector3(-1, 0, 0))
  1. if (Input.IsActionPressed("movement_forward"))
  2. node.Translate(new Vector3(0, 0, 1));
  3. if (Input.IsActionPressed("movement_backward"))
  4. node.Translate(new Vector3(0, 0, -1));
  5. if (Input.IsActionPressed("movement_left"))
  6. node.Translate(new Vector3(1, 0, 0));
  7. if (Input.IsActionPressed("movement_right"))
  8. node.Translate(new Vector3(-1, 0, 0));

Note

Notice how we do not need to do any calculations to get world space directional vectors. We can define a few Vector3 variables and input the values pointing in each direction.

Here is what world space looks like in 2D:

Note

The following images are just examples. Each arrow/rectangle represents a directional vector

../../../_images/WorldSpaceExample.png

And here is what it looks like for 3D:

../../../_images/WorldSpaceExample_3D.png

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.

Note

This question from Game Development Stack Exchange has a much better explanation of world space and local space.

https://gamedev.stackexchange.com/questions/65783/what-are-world-space-and-eye-space-in-game-development (Local space and eye space are essentially the same thing in this context)

To get a Spatial node’s local space, we need to get its Transform, so then we can get the Basis from the Transform.

Each Basis has three vectors: X, Y, and 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:

GDScript

C#

  1. if Input.is_action_pressed("movement_forward"):
  2. node.translate(node.global_transform.basis.z.normalized())
  3. if Input.is_action_pressed("movement_backward"):
  4. node.translate(-node.global_transform.basis.z.normalized())
  5. if Input.is_action_pressed("movement_left"):
  6. node.translate(node.global_transform.basis.x.normalized())
  7. if Input.is_action_pressed("movement_right"):
  8. node.translate(-node.global_transform.basis.x.normalized())
  1. if (Input.IsActionPressed("movement_forward"))
  2. node.Translate(node.GlobalTransform.basis.z.Normalized());
  3. if (Input.IsActionPressed("movement_backward"))
  4. node.Translate(-node.GlobalTransform.basis.z.Normalized());
  5. if (Input.IsActionPressed("movement_left"))
  6. node.Translate(node.GlobalTransform.basis.x.Normalized());
  7. if (Input.IsActionPressed("movement_right"))
  8. node.Translate(-node.GlobalTransform.basis.x.Normalized());

Here is what local space looks like in 2D:

../../../_images/LocalSpaceExample.png

And here is what it looks like for 3D:

../../../_images/LocalSpaceExample_3D.png

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.

Note

You can change between local and world space modes by pressing T or the little cube button when you have a Spatial based node selected.

../../../_images/LocalSpaceExampleGizmo.png

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 process_input:

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.

Note

This may seem overkill for just the keyboard, but this will make sense later when we add joypad input.

Based on which directional movement action is pressed, we add to or subtract from input_movement_vector.

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 input_movement_vector.y to 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.

Note

Because the camera is rotated by -180 degrees, we have to flip the Z directional vector. Normally forward would be the positive Z axis, so using basis.z.normalized() would work, but we are using -basis.z.normalized() because our camera’s Z axis faces backwards in relation to the rest of the player.

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 JUMP_SPEED.

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 process_movement:

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 Y velocity.

After that we assign the player’s velocity to a new variable (called hvel) and remove any movement on the Y axis.

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 dir.

After that we make a new variable for acceleration, named accel.

We then take the dot product of hvel to see if the player is moving according to hvel. Remember, 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 X and Z velocity to the interpolated horizontal velocity, and call move_and_slide to let the KinematicBody handle moving the player through the physics world.

Tip

All the code in process_movement is exactly the same as the movement code from the Kinematic Character demo!


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.

Note

See Mouse and input coordinates for a list of possible input events.

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 X value.

Tip

Godot converts relative mouse motion into a Vector2 where mouse movement going up and down is 1 and -1 respectively. Right and Left movement is 1 and -1 respectively.

Because of how we are rotating the player, we multiply the relative mouse motion’s X value by -1 so mouse motion going left and right rotates the player left and right in the same direction.

Finally, we clamp the rotation_helper‘s X rotation to be between -70 and 70 degrees so the player cannot rotate themselves upside down.

Tip

See using transforms for more information on rotating transforms.


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:

GDScript

C#

  1. const MAX_SPRINT_SPEED = 30
  2. const SPRINT_ACCEL = 18
  3. var is_sprinting = false
  4. var flashlight
  1. [Export]
  2. public float MaxSprintSpeed = 30.0f;
  3. [Export]
  4. public float SprintAccel = 18.0f;
  5. private bool _isSprinting = false;
  6. private SpotLight _flashlight;

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 _ready:

GDScript

C#

  1. flashlight = $Rotation_Helper/Flashlight
  1. _flashlight = GetNode<SpotLight>("Rotation_Helper/Flashlight");

This gets the Flashlight node and assigns it to the flashlight variable.


Now we need to change some of the code in process_input. Add the following somewhere in process_input:

GDScript

C#

  1. # ----------------------------------
  2. # Sprinting
  3. if Input.is_action_pressed("movement_sprint"):
  4. is_sprinting = true
  5. else:
  6. is_sprinting = false
  7. # ----------------------------------
  8. # ----------------------------------
  9. # Turning the flashlight on/off
  10. if Input.is_action_just_pressed("flashlight"):
  11. if flashlight.is_visible_in_tree():
  12. flashlight.hide()
  13. else:
  14. flashlight.show()
  15. # ----------------------------------
  1. // -------------------------------------------------------------------
  2. // Sprinting
  3. if (Input.IsActionPressed("movement_sprint"))
  4. _isSprinting = true;
  5. else
  6. _isSprinting = false;
  7. // -------------------------------------------------------------------
  8. // -------------------------------------------------------------------
  9. // Turning the flashlight on/off
  10. if (Input.IsActionJustPressed("flashlight"))
  11. {
  12. if (_flashlight.IsVisibleInTree())
  13. _flashlight.Hide();
  14. else
  15. _flashlight.Show();
  16. }

Let’s go over the additions:

We set is_sprinting to 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 is_sprinting variable.

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:

GDScript

C#

  1. if is_sprinting:
  2. target *= MAX_SPRINT_SPEED
  3. else:
  4. target *= MAX_SPEED
  1. if (_isSprinting)
  2. target *= MaxSprintSpeed;
  3. else
  4. target *= MaxSpeed;

Now instead of always multiplying target by MAX_SPEED, we first check to see if the player is sprinting or not. If the player is sprinting, we instead multiply target by MAX_SPRINT_SPEED.

Now all that’s left is to change the acceleration when sprinting. Change accel = ACCEL to the following:

GDScript

C#

  1. if is_sprinting:
  2. accel = SPRINT_ACCEL
  3. else:
  4. accel = ACCEL
  1. if (_isSprinting)
  2. accel = SprintAccel;
  3. else
  4. 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!

Final notes

../../../_images/PartOneFinished.png

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.

Note

At this point we’ve recreated the Kinematic character demo from first person perspective with sprinting and a flash light!

Tip

Currently the player script would be at an ideal state for making all sorts of first person games. For example: Horror games, platformer games, adventure games, and more!

Warning

If you ever get lost, be sure to read over the code again!

You can download the finished project for this part here: Godot_FPS_Part_1.zip