Character movement
For a game where you have a character that can walk, run, jump, etc, you will need some generic character movement functionality. If you want to have complete control, you could implement this yourself, but there is also a built in character movement component that you can attach to your character actor.
Example
The actor implementation below illustrates how you can attach the CharacterMovementComponent
to an actor and pass in various properties to configure its behaviours. In the example we are also attaching a mesh component to give the character a visual representation. Also, the third party camera component is attached to have a camera following the character.
You likely want to tweak the parameters of the character movement component a lot to find something that works well for your game.
In this example, we make the movement component instance public because it provides an interface to control movement which we want to be accessible from a player controller.
Controlling the movement
With the above code, the character will simply fall to the ground where it is spawned and not move. To control the character you need to bind various action inputs that are exposed on the movement component.
directionInput - For controlling the movement of the character forward, backward, left and right
jumpInput - Toggle when you want the character to jump
sprintInput - Toggle to change the movement speed to the configured sprinting speed
rotationInput - Rotate the character around the y-axis to turn left and right.
The code example below illustrates how code could look inside a player controller using an injected InputService
and a referenced character actor with a movement component.
Read more about how to handle player input in the Player input guide.
Last updated