Home > Articles

  • Print
  • + Share This
This chapter is from the book

Controlling a Pawn’s Movement

UE4 makes controlling a Pawn’s movement very easy. Because you inherited your Pawn from the DefaultPawn class, all the heavy lifting has already been done. To see just how simple it is to control your Pawn’s movement, you can test your work.

First, you need to tell the Game Mode to spawn the player using your new Hero_Spaceship Pawn by default. You set this in the class Defaults panel in the Game Mode’s Blueprint Class Editor or in the Maps & Modes section of the Project Settings panel.

In the next Try It Yourself, you set the Hero_Spaceship Pawn class as the default Pawn class in ArcadeShooter_GameMode. You also set the Player Controller class to Hero_PC.

With Hero_Spaceship set up to be the Game Mode’s default Pawn, you are ready to test your Pawn’s movement. In the Level Editor toolbar, click Play, as shown in Figure 20.5. When the game starts, use the arrow keys or WSAD to move around. You can also use the mouse to look around. When you are done, press the Esc key to stop.

FIGURE 20.5

FIGURE 20.5 Click the Play button on the toolbar to instantly test your game while staying in the Editor.

Although your Pawn can move freely, a couple things don’t seem to match your design brief. First, the camera is first person instead of top-down and fixed. Second, your Pawn is moving forward and backward as well as side to side. In this case, you want to pull back from all the features that UE4 has provided you and put in some logic to lock things down.

Disabling the Default Movement

The DefaultPawn class does a lot automatically, but in this case, you want to more manual control. Luckily, it’s pretty simple to get that control. The DefaultPawn class’s Defaults panel contains a property called Add Default Movement Bindings, which is selected by default. By unselecting this property, you can disable the DefaultPawn class’s basic movement and overwrite its behavior and bindings with your own (see Figure 20.6).

FIGURE 20.6

FIGURE 20.6 In the Class Defaults of the Pawn, disable the Add Default Movement Bindings check box.

Setting Up Input Action and Axis Mappings

A locked spaceship isn’t exactly what you want. It looks like you quickly swung from too much freedom to none at all, and you need to add back some user control. One part of this is binding different keypresses to different actions. Taking an input—like a joystick movement, a keypress, or a trigger pull—and registering a specific action with that input is called input binding, and you do this at the Project level.

To set input binding, select Settings > Project Settings and then open the Input section of the Project Settings panel. At the top of this section are two lists in the Bindings section: Action Mappings and Axis Mappings. The difference between these two sections is subtle but important. Action mappings are for single keypress and release inputs. These are usually used for jumping, shooting, and other discrete events. Axis mappings are for continuous input, such as movement, turning, and camera control. Both types of mappings can be used simultaneously and picking the right type of binding for your actions will make creating complex and rich player interactions easier.

Axis mappings work slightly differently depending on the hardware generating an input. Some hardware (such as mice, joysticks, or gamepads) return input values to UE4 in a range from –1 to 1. UE4 can scale that value, depending on how much the user wants to let the input influence the game. Keyboards, however, separate up and down and left and right to different keys and don’t provide a continuous range of input. A key is either pressed or it isn’t, so when you’re binding a key as an axis mapping, UE4 needs to be able to interpret that pressed key as a value on that same –1 to 1 scale.

For movement, you use axis mappings, and in your arcade shooter, you are limiting the player’s movement to a single axis, so the player can move either left or right. In the next Try It Yourself, you set up the input bindings to support left and right movement for your Pawn.

At the top of the Axis Mappings properties in the Project Settings panel is a field where you input the name for the action that is to be performed. You click the + symbol beside the action name to add a new binding. Each binding has two parts: the input that is being bound and a scale next to it that modulates the result.

You want the game to treat keypresses, like A and D, as a continuous axis. To do this, you need to have some of those keys be negative; in other words, when you press left, you want the axis to go down, and when you press right, you want the axis to go up.

For thumbstick axes (e.g., Gamepad Left Thumbstick X-Axis), the negative values are already calculated, so the scale should usually just be 1.0.

In this example, the keys A and D, the left arrow and right arrow keys, and the Gamepad Left Thumbstick are all being bound to the MoveRight action. This brings up an important distinction: By using Action Mappings and Axis Mappings, you can bind multiple different input methods to the same event. This means less testing and duplication of Blueprint scripts in your project, and it means everything becomes more readable. Instead of having Blueprint scripts checking whether the A key is pressed, the Blueprint can just update movement when the MoveRight event is triggered.

But just creating an input binding doesn’t make things move. Now you need to actually use the MoveRight action.

Using Input Events to Move a Pawn

You are now ready to set up movement again. You have a fancy input axis called MoveRight and a Pawn that is just itching to move again. First, you need to open the Blueprint Class Editor of your Pawn and go to the Event Graph. Here, you can begin to lay down behaviors that will fire when your MoveRight action is triggered.

In the Event Graph, right-clicking and searching for your action by name, MoveRight, brings up the InputAxis MoveRight event into the graph, as shown in Figure 20.8.

FIGURE 20.8

FIGURE 20.8 Axis mappings show up by name under Axis Events. There are also Axis Values functions and Pawn functions, but these functions are not what you are looking for in this case.

Once you have an axis event, you can query the axis value and convert it into movement. To do this you need a few more Blueprint nodes, starting with Add Movement Input. This function works with MovementComponent to interpret a value and a world space direction to move the Pawn in.

By hooking up the InputAxis MoveRight event’s execution pin and the value that is returned in Axis Value to Add Movement Input, MovementComponent can take the player’s inputs and move the Pawn in a world direction.

Since you want the spaceship to move left or right on input, you need to take the vector coming from the Pawn’s right axis. You can get this vector by using the Get Actor Right Vector node and plugging its Return Value into the Add Movement Input’s World Direction (see Figure 20.9).

FIGURE 20.9

FIGURE 20.9 The finished graph of the Add Movement Input.

  • + Share This
  • 🔖 Save To Your Account