Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Game Development Projects with Unreal Engine

You're reading from  Game Development Projects with Unreal Engine

Product type Book
Published in Nov 2020
Publisher Packt
ISBN-13 9781800209220
Pages 822 pages
Edition 1st Edition
Languages
Authors (4):
Hammad Fozi Hammad Fozi
Profile icon Hammad Fozi
Gonçalo Marques Gonçalo Marques
Profile icon Gonçalo Marques
David Pereira David Pereira
Profile icon David Pereira
Devin Sherry Devin Sherry
Profile icon Devin Sherry
View More author details

Table of Contents (19) Chapters

Preface
1. Unreal Engine Introduction 2. Working with Unreal Engine 3. Character Class Components and Blueprint Setup 4. Player Input 5. Line Traces 6. Collision Objects 7. UE4 Utilities 8. User Interfaces 9. Audio-Visual Elements 10. Creating a SuperSideScroller Game 11. Blend Spaces 1D, Key Bindings, and State Machines 12. Animation Blending and Montages 13. Enemy Artificial Intelligence 14. Spawning the Player Projectile 15. Collectibles, Power-Ups, and Pickups 16. Multiplayer Basics 17. Remote Procedure Calls 18. Gameplay Framework Classes in Multiplayer

4. Player Input

Overview

This chapter will tackle the subject of player input. We will learn how to associate a keypress or touch input from a touch-enabled device with an in-game action such as jumping or moving.

By the end of this chapter, you will know about Action Mappings and Axis Mappings, how to create and modify them, how to listen to each of those mappings, how to execute in-game actions when they're pressed and released, and how to preview your game as if you were playing on a mobile device.

Introduction

In the previous chapter, we created our C++ class that inherits from the Character class and added all the necessary Actor components to be able to see the game from that character's perspective, as well as being able to see the character itself. We then created a Blueprint class that inherits from that C++ class, in order to visually set up all its necessary components. We also learned briefly about Action and Axis Mappings.

In this chapter, we will be going more in-depth on these topics, as well as covering their C++ usage. We will learn about how player input works in UE4, how the engine handles input events (key presses and releases), and how we can use them to control logic in our game.

Let's start this chapter by getting to know how UE4 abstracts the keys pressed by the player to make it easier for you to be notified of those events.

Note

In this chapter, we will be using an alternative version of the Character blueprint we created, called...

Input Actions and Axes

Player input is the thing that distinguishes video games from other entertainment media: the fact that they're interactive. For a video game to be interactive, it must take into account the player's input. Many games do this by allowing the player to control a virtual character that acts upon the virtual world it's in, depending on the keys and buttons that the player presses, which is exactly what we'll be doing in this chapter.

Most game development tools nowadays allow you to abstract keypresses into Actions and Axes, which allow you to associate a name (for example, Jump) with several different player inputs (pressing a button, flicking a thumbstick, and so on). The difference between Actions and Axes is that Actions are used for binary inputs (inputs that can either be pressed or released, like the keys on the keyboard), while Axes are used for inputs that are scalar or continuous (that is, that can have a range of values, like...

Processing Player Input

Let's think about a situation where the player presses the Jump action, which is associated with the Spacebar key, to get the player character to jump. Between the moment the player presses the Spacebar key and the moment the game makes the player character jump, quite a few things have to connect those two events.

Let's take a look at all the steps necessary that lead from one event to the other:

  1. Hardware Input: The player presses the Spacebar key. UE4 will be listening to this keypress event.
  2. The PlayerInput class: After the key is pressed or released, this class will translate that key into an action or axis. If there is an action or axis associated with that key, it will notify all classes that are listening to the action that it was just pressed, released, or updated. In this case, it will know that the Spacebar key is associated with the Jump action.
  3. The Player Controller class: This is the first class to receive these events...

DefaultInput.ini

If you go to your project's directory, using File Explorer, and then open its Config folder, you'll find some .ini files in it, one of which should be the DefaultInput.ini file. As the name suggests, this file holds the main settings and configuration for input-related properties.

In the first exercise of this chapter, where we edited the project's Input settings, what was happening, in reality, was that the editor was writing to and reading from the DefaultInput.ini file.

Open this file in a text editor of your choice. It contains many properties, but the ones we want to take a look at now are the list of Action Mappings and Axis Mappings. Near the end of the file, you should see, for instance, the Jump action being specified in this file:

+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,  bAlt=False,bCmd=False,Key=SpaceBar)
+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,  bAlt=False,bCmd=False,Key...

Turning the camera around the character

Cameras are an extremely important part of games, as they dictate what and how the player will see your game throughout the play session. When it comes to third-person games, which is the case for this project, the camera allows you not only to see the world around them but also the character you're controlling. Whether the character is taking damage, falling, or something else, it's important for the player to always know the state of the character they are controlling and to be able to have the camera face the direction they choose.

Much like with every modern, third-person game, we will always have the camera rotate around our player character. To have our camera rotate around our character, after setting up the Camera and Spring Arm components in Chapter 2, Working with Unreal Engine, let's continue by adding two new Axis Mappings, the first one called Turn, which is associated with the Gamepad Right Thumbstick X-Axis and...

Summary

In this chapter, you've learned how to add, remove, and modify Action Mappings and Axis Mappings, which give you some flexibility when determining which keys trigger a specific action or axis, how to listen to them, and how to execute in-game logic when they're pressed and released.

Now that you know how to handle the player's input, you can allow the player to interact with your game and offer the agency that video games are so well known for.

In the next chapter, we'll start making our own game from scratch. It'll be called Dodgeball and will consist of the player controlling a character trying to run away from enemies that are throwing dodgeballs at it. In that chapter, we will have the opportunity to start learning about many important topics, with a heavy focus on collisions.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Game Development Projects with Unreal Engine
Published in: Nov 2020 Publisher: Packt ISBN-13: 9781800209220
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}