Character movement programming
Last updated
Last updated
Level: Beginner
In this tutorial we will explain how to setup a playable human-like character with keyboard controls and animations that are synced to the movement. This is similar to the Starter project - Third person shooter and we will go into details of how to set these up so that you can understand what is happening and how to extend it.
The complete code for this project can be cloned from GitHub.
https://github.com/hologyengine/movement-tutorial
Before continuing, you need to create a new project and familiarise yourself with the basics of the editor. Go through the article The first steps to learn how to set up a project. Then learn how to navigate in the editor using the articles in Editor basics.
While some coding skills are useful, all the code needed will be provided in this tutorial.
After creating the project, you have an empty scene that you can use for this tutorial. We will just create a couple of objects in this scene to illustrate how to make a character. In order for our character to have something to walk on, we will add a landscape shape. We will also add a spawn point actor so that we can spawn the playable character in the scene from game code.
In the Asset browser, select Shapes and scroll down to find the Landscape item.
Right click the Landscape item click "Add to scene". You will be prompted with some options for creating the landscape. For the purpose of this tutorial, you can leave the defaults as they are and click "Create".
Next, we need to create a spawn point. In the Asset browser, select Actors and then right click Spawn Point and add it to the scene. Click anywhere on the landscape you created earlier to place the spawn point.
We need a 3D models for our character that has animations that we can play. For this tutorial, we will use a character from an asset pack made by Kenney.
Once you have downloaded the asset pack, unzip the file, find one of the character models such as Models/GLB format/character-human.glb and drag it into the editor to import it. We will later refer to this asset from game code.
We will now create an actor class to represent the playable character. Create a file in src/actors/character.ts. Add the code below to the file.
First we attach a few components to quickly add a lot of functionality to our actor.
CharacterAnimationComponent allows us to play animations using a state machine. Read more about Character Animation
CharacterMovementComponent can take player input and move the character with common modes such as standing idle, walking, running and jumping. Read more about Character movement
ThirdPartyCameraComponent moves the camera to behind the character. It provides options to offset the camera in various ways.
We load an asset using the injected AssetLoader using the name of the asset as can be found in the editor. This gives us a model's scene which is the character mesh and an array of animations.
In the rest of the code we set up the animation state machine. Read the guide on Animation State Machine to get a better understanding of how these work.
To be able to control the character's movement, we will create a player controller service that takes the player's input and forwards it to the character's movement component.
Create a file at src/services/player-controller.ts with the following code.
This code uses the InputService. To get a better understanding of how this works, read the Player input guide.
Now we just need to spawn the character and setup the player controller which we can do from the game class found in src/services/game.ts. Replace the contents of this file with the following.
In this code we do a few things in the onStart function which gets called after the game has loaded and is ready to start.
We find a spawn point actor in the world.
Spawn the character using the spawn point and the Character actor class created earlier to get a new character that is placed in the scene with the position and rotation of the spawn point.
Call the setup function on the player controller with the character to bind the input events to this character's movement component.
This is everything necessary to have a playable character with animations.
Run the following command in the terminal to run the game.
This should give you a link such as http://localhost:5173 that you can open in your browser to see the game.
You should now see the game running like in the picture at the start of this tutorial. Try pressing W,A,S,D on your keybaord. Click somewhere in the scene with your mouse first to be able to rotate the character and camera by moving your mouse.
You now have a starting point of what could be a super fun game.
Read more about the features covered in this tutorial by following the links.
Design a more interesting world by importing more assets and place them in your scene
Create a more interesting game. For example, add some game play mechanisms such as Trigger volumes that could trigger various things as you walk into them them or place other actors that does something when you are nearby and press a button.