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

5. Line Traces

Overview

This chapter will be the start of a new game project called Dodgeball, where we will be creating a game from scratch that features mechanics based on collision concepts. In this chapter, you will modify the Third Person Template project to give it a top-down perspective. Then, you will be introduced to line traces, a key concept in game development, and learn about their potential and use cases.

By the end of this chapter, you will be able to use UE4's built-in Line Trace feature (also known as Raycasts or Raytraces in other game development tools) by executing different types of line traces; creating your own trace channels; and modifying an object's response to each trace channel.

Introduction

In the previous chapters, we learned how we can reproduce the Third Person Template project offered to us by the Unreal Engine team in order to understand some of the basic concepts of UE4's workflow and framework.

In this chapter, you will start creating another game from scratch. In this game, the player will control a character from a top-down point of view (similar to games such as Metal Gear Solid 1, 2, and 3). A top-down perspective implies that the player controls a character that is seen as if it was being looked down upon, usually with the camera rotation being fixed (the camera doesn't rotate). In our game, the player character must go from point A to point B without being hit by dodgeballs that are being thrown at the player by the enemies that are spread throughout the level. The levels in this game will be maze-like in nature, and the player will have multiple paths to choose from, all of which will have enemies trying to throw dodgeballs at the...

Collision

A collision is basically a point at which two objects come into contact with each other (for example, two objects colliding, an object hitting a character, a character walking into a wall, and so on). Most game development tools have their own set of features that allow for collision and physics to exist inside the game. This set of features is called a Physics Engine, which is responsible for everything related to collisions. It is responsible for executing Line Traces, checking whether two objects are overlapping each other, blocking each other's movement, bouncing off of a wall, and much more. When we ask the game to execute or notify us of these collision events, the game is essentially asking the Physics Engine to execute it and then show us the results of these collision events.

In the Dodgeball game you will be building, examples of where collision needs to be taken into account include checking whether enemies are able to see the player (which will be achieved...

Project Setup

Let's begin this chapter by creating our Unreal Engine project:

  1. Launch UE4. Select the Games project category, then press Next.
  2. Select the Third Person template, then press Next.
  3. Make sure the first option is set to C++ and not Blueprint.
  4. Select the location of the project according to your preference and name your project Dodgeball, then press Create Project.

    When the project is done being generated, you should see the following on your screen:

    Figure 5.1: Dodgeball project loaded up

  5. After the code has been generated and the project opens up, close the UE4 editor and open the files of the generated third-person Character class, DodgeballCharacter, in Visual Studio, as shown in the following figure:

Figure 5.2: Files generated in Visual studio

As mentioned before, your project is going to have a top-down perspective. Given that we're starting this project from the Third Person Template, we'll have...

Line Traces

One of the most important features of any game development tool is its ability to execute Line Traces. These are available through the Physics Engine that the tool is using.

Line Traces are a way of asking the game to tell you whether anything stands between two points in the game world. The game will shoot a ray between those two points, specified by you, and return the objects that were hit (if any), where they were hit, at what angle, and much more.

In the following figure, you can see a representation of a Line Trace where we assume object 1 is ignored and object 2 is detected, due to their Trace Channel properties (further explained in the following paragraphs):

Figure 5.4: A Line Trace being executed from point A to point B

In Figure 5.4:

  • The dashed line represents the Line Trace before it hits an object.
  • The arrows represent the direction of the Line Trace.
  • The dotted line represents the Line Trace after it hits...

Creating the EnemyCharacter C++ class

In our Dodgeball game, the EnemyCharacter class will constantly be looking at the player character, if they're within view. This is the same class that will later throw dodgeballs at the player; however, we'll leave that to the next chapter. In this chapter, we will be focusing on the logic that allows our enemy character to look at the player.

So, let's get started:

  1. Right-click the Content Browser inside the editor and select New C++ Class.
  2. Choose the Character class as the parent class.
  3. Name the new class EnemyCharacter.

After you've created the class and opened its files in Visual Studio, let's add the LookAtActor function declaration in its header file. This function should be public, not return anything and only receive the AActor* TargetActor parameter, which will be the actor it should be facing. Have a look at the following code snippet, which shows this function:

// Change the...

Visualizing the Line Trace

When creating new logic that makes use of Line Traces, it is very useful to actually visualize the Line Trace while it's being executed, which is something that the Line Trace function doesn't allow you to do. In order to do that, we must use a set of helper debug functions that can draw objects dynamically at runtime, such as lines, cubes, spheres, and so on.

Let's then add a visualization of our Line Trace. The first thing we must do in order to use the debug functions is to add the following include below our last include line:

#include "DrawDebugHelpers.h"

We will want to call the DrawDebugLine function in order to visualize the Line Trace, which needs the following inputs, very similar to the ones received by the Line Trace function:

  1. The current World, which we will supply with the GetWorld function
  2. The Start and End points of the line, which will be the same as the LineTraceSingleByChannel function...

Creating the EnemyCharacter Blueprint Class

Now that we have finished the logic for our EnemyCharacter C++ class, we must create our blueprint class that derives from it:

  1. Open our project in the Editor.
  2. Go to the Blueprints folder inside the ThirdPersonCPP folder, in the Content Browser.
  3. Right-click and select the option to create a new blueprint class.
  4. Expand the All Classes tab near the bottom of the Pick Parent Class window, search for our EnemyCharacter C++ class, and select it as the parent class.
  5. Name the Blueprint class BP_EnemyCharacter.
  6. Open the Blueprint class, select the SkeletalMeshComponent (called Mesh) from the Components tab, and set its Skeletal Mesh property to SK_Mannequin and its Anim Class property to ThirdPerson_AnimBP.
  7. Change the Yaw of SkeletalMeshComponent to -90º (on the z-axis) and its position on the z-axis to -83 units.
  8. After you've set up the Blueprint class, its mesh setup should look very similar to...

Sweep Traces

Before we continue with our project, it is important to know about a variant of the Line Trace, which is the Sweep Trace. Although we won't be using these in our project, it is important to know about them and how to use them.

While the Line Trace basically shoots a ray between two points, the Sweep Trace will simulate throwing an object between two points in a straight line. The object that is being thrown is simulated (doesn't actually exist in the game) and can have various shapes. In the Sweep Trace, the Hit location will be the first point at which the virtual object (which we will call Shape) hits another object, if it were thrown from the start point to the end point. The shapes of the Sweep Trace can be either a box, a sphere, or a capsule.

Here is a representation of a Sweep Trace from point A to point B, where we assume that object 1 is ignored due to its Trace Channel properties, using a box shape:

Figure 5.8: Representation...

Summary

By completing this chapter, you have added a new tool to your belt: Line Traces. You now know how to execute Line Traces and Sweep Traces, both Single and Multi; how to change an object's response to a specific Trace Channel; and how to create your own Trace Channels.

You will quickly realize in the chapters ahead that these are essential skills when it comes to game development, and you will make good use of them on your future projects.

Now that we know how to use Line Traces, we're ready for the next step, which is Object Collision. In the next chapter, you will learn how to set up collisions between objects and how to use collision events to create your own game logic. You will create the Dodgeball Actor, which will be affected by real-time physics simulation; the Wall Actors, which will block both the characters' movements and the dodgeball; and the Actor responsible for ending the game when the player comes into contact with it.

...
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}