When we create a new default project with Unreal Engine, it uses the DefaultPawn which is a kind of free camera, i we can move in all directions without being affected by the gravity. In this mode, the keyboard’s keys are used to move the pawn, while the mouse is used to set its orientation. Usually, it’s W to go forward, A to go left and so on. But this works only if we have a QWERTY keyboard layout, if we have an AZERTY keyboard (French layout) then the position of the keys used to move isn’t natural.
The WASD controls are good for the QWERTY layout, but may be really weird for another layout.
So far (at least in UE 4.18), I have not found an easy way to edit the controls of the default pawn easily. It seems that it doesn’t rely on the input system, so we can’t change the binding of the axis. It may be fixed in the future, or maybe I don’t have found the way to do it, but anyway if you’re interested into define your own pawns, the rest of the article may still be useful.
Indeed, the easiest way I found to define controls of the DefaultPawn is to define our own Pawn based on the input system. We will see how to do it in this short tutorial. Implementing this is pretty easy and straightforward and it’s a good introduction on how to define custom pawns with custom inputs.
A good starting point is to read the Unreal tutorial on the input system here.
In our case, we will just have to define 3 axes for the movements (MoveForward, MoveRight and MoveUp) and two axes for the rotation (RotationX, RotationY). When working with a QWERTY keyboard, we would use S-W for the MoveForward axis, A-D for MoveRight, and eventually Z-C for MoveUp. With an AZERTY keyboard, it would be better to use S-Z/MoveForward, Q-D/MoveRight, W-C/MoveUp. The mouse axes are just bound on the mouse events.
The result is something like this:
Creating the pawn.
Now that we have clearly defined our inputs, we will create a new pawn using these inputs and reacting to them. First, let’s add a new Blueprint Class (Add New -> Blueprint Class) and let’s select Pawn for the type of class. This new pawn will be our free camera, so let’s call it FreeCameraPawn.
As far as I know, any pawn have a camera attached, so we don’t need to attach a camera to the pawn: the position and the rotation of this actor will be the position/rotation of the camera.
Now, we need to bind the inputs to movements. It is done in the Event Graph. We will detail how to manage the MoveForward axis, and then, it will be the same for MoveRight and MoveUp. So let’s right-click anywhere in the editor and look for “MoveForward”, we should then see this:
Click on the MoveForward under the category Axis Events to create the event node. We will then connect it to the Add Movement Input node. This node is used to create a movement vector (but doesn’t apply it): it takes as parameters the world direction of the movement (here it will be the forward vector of the actor), and the scale value. So basically, we can have this:
And for the other axes, we just have to do the same but with the right vector (for MoveRight) and the up vector (for MoveUp).
For RotationX and RotationY inputs, it’s slightly different. We will not use Add Movement Input, but Add Controller Yaw Input for RotationX and Add Controller Pitch Input for RotationY.
For rotation, we don’t have to do anything else, it should work. But for movement it’s more complicated. We are just creating the movement vectors, but we are not applying them. To do so, we have to consume the movement vector in the Event Tick. It can be done like this:
And after this, our pawn is ready. If we look back at this blueprint code, we can see it’s pretty simple.
Using our newly created pawn.
A small step remains before being able to use the new pawn: we need to replace the default pawn by the FreeCameraPawn. First, we create a new GameMode (Add New -> Blueprint Class -> Game Mode), and in this Game Mode we set the Default Pawn Class to FreeCameraPawn.
Once the Game Mode is created, open it and locate the Default Pawn Class field, replace DefaultPawn with FreeCameraPawn
Then, in World Settings we override the Game Mode by the newly created Game Mode.
Here, Game Mode should be replaced by your newly created Game Mode
We can now test our program, and it works (we can move the camera freely using the inputs we defined). But it’s slow…
To overcome this, there are two main solutions:
- Scaling the value of Axis Value in the events, before sending it into the Add Movement Input nodes.
- Changing the axis scale value in the inputs.
Selecting one instead of the other may depend of the use case. For instance, if we want to dynamically change this scaling at runtime, we will take the first solution. If not, the second one is sufficient.
This small tutorial covers quickly the usage of the input system and the creation of pawns by the example of the Free Camera Pawn. There are a lot of possibilities remaining (we didn’t talk about actions and we chose to avoid talking about controllers for the moment), but it’s enough to manage any basic camera movement (free camera, orbit camera, etc.) so that we can implement them in games. As always, if you have any remark, question or suggestion, feel free to comment this article.