Reader small image

You're reading from  Game Development Projects with Unreal Engine

Product typeBook
Published inNov 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781800209220
Edition1st Edition
Languages
Tools
Right arrow
Authors (4):
Hammad Fozi
Hammad Fozi
author image
Hammad Fozi

Hammad Fozi comes from a gaming background and has been extensively working on Unreal Engine since 2017. He has been part of some very successful AAA projects such as Virtua FanCave (and Metaverse), Unnamed AAA Sci-Fi DJ Experience, Heroes and Generals, and Creed: Rise to Glory VR. Hammad has worked with teams who have had experience working at Ubisoft, Warner Bros. Games, 2K Games, and more! He has successfully helped teams consisting of 10–30 people to scale to 150+ in size over his very short yet impressive career. Hammad currently works as a senior C++ game developer and has extensive experience in working with VR and augmented reality, PC/PS5/Xbox/Android/iOS/macOS game development, and Web3/Metaverse/NFT systems (within Unreal Engine).
Read more about Hammad Fozi

Gonçalo Marques
Gonçalo Marques
author image
Gonçalo Marques

Gonçalo Marques has been an active gamer since the age of 6. He has been using Unreal Engine since 2016 and has done freelance and consulting work using the engine. Gonçalo also released a free and open source plugin called UI Navigation, which has garnered an extremely positive reception with over 100,000 downloads and is still receiving frequent updates and fixes. Thanks to the development of this plugin, he became an Epic MegaGrant recipient. He is now working at Funcom ZPX, a game studio in Lisbon that has contributed to games such as Conan Exiles, Mutant Year Zero, and Moons of Madness. Gonçalo is currently working on a new Funcom game in the Dune universe.
Read more about Gonçalo Marques

David Pereira
David Pereira
author image
David Pereira

David Pereira started making games in 1998 when he learned how to use Clickteam's The Games Factory. He graduated in computer science from FCT-UNL, where he learned about C++, OpenGL, and DirectX, which allowed him to create more complex games. After working in IT consulting for a few years, he joined Miniclip in Portugal where he worked on popular mobile games such as 8 Ball Pool, Gravity Guy 1 and Gravity Guy 2, Extreme Skater, iStunt2, Hambo, and many others. Since then, he has been the lead developer for MPC in the John Lewis Christmas VR Experience, worked on an earlier version of Mortal Shell, and did volunteer work teaching people with Asperger's how to make games with Unreal Engine 4. Today, he's working on his own game, a soon-to-be-announced first-person action RPG.
Read more about David Pereira

Devin Sherry
Devin Sherry
author image
Devin Sherry

Devin Sherry is originally from Levittown, NY, located on Long Island. He studied the topics of Game Development and Game Design at the University of Advancing Technology where he had earned his Bachelor of Arts in Game Design in 2012. During his time in college, Devin worked as a game and level designer with a group of students called Autonomous Games on a real-time strategy styled, third-person shooter called The Afflicted using Unreal Engine 3/UDK where it was presented at GDC in 2013 at the GDC Play Showcase. Today, Devin works as an independent game developer located in Tempe, Arizona, where he works on personal and contracted projects. His achievements include the title Radial Impact, which can be found in the Community Contributions section of the Learn Tab of Unreal Engine 4's Launcher, and his work on his YouTube Channel, Devin Level Design, where he educates viewers on game development within Unreal Engine 3, UDK, and Unreal Engine 4.
Read more about Devin Sherry

View More author details
Right arrow

13. Enemy Artificial Intelligence

Overview

This chapter begins with a brief recap of how the enemy AI will behave for the SuperSideScroller game. From there, you will learn about Controllers in the context of Unreal Engine 4 and learn how to create an AI Controller. Then, you will learn more about AI navigation in Unreal Engine 4 by adding a Nav Mesh to the main level of the game.

By the end of this chapter, you will be able to create a navigable space where the enemy can move. You will also be able to create an enemy AI pawn and navigate it across locations using Blackboard and behavior trees. Lastly, you will know how to create and implement a player projectile class and add visual elements to it.

Introduction

In the previous chapter, you added layered animations for the player character using Animation Blending with a combination of Anim Slots, Animation Blueprints, and blending functions such as Layered Blend per Bone.

In this chapter, you will learn how to use a Nav Mesh to create a navigable space inside of the game world that the enemy can move in. Defining the navigable space of a level is crucial for allowing artificial intelligence (AI) to access and move to specific areas of your level.

Next, you will create an enemy AI pawn that can navigate between patrol point locations inside the game world using a combination of the AI tools present inside Unreal Engine 4, including Blackboards and behavior trees.

You will also learn how to use a Nav Mesh to create a navigable space inside the game world where the enemy can move. Defining the navigable space of a level is crucial for allowing the AI to access and move to specific areas of your level. 

Lastly,...

Enemy AI

What is AI? This term can mean many things, depending on the field and context that it is used in, so let's define it in a way that makes sense regarding the subject of video games.

AI is an entity that is aware of its environment and performs choices that will help optimally achieve its intended purpose. AI uses what are called finite state machines to switch between more than one state based on the input it receives from the user or its environment. For example, a video game AI can switch between an offensive state to a defensive state based on its current health.

In games such as Hello Neighbor, which was developed in Unreal Engine 4, and Alien: Isolation, the goal of the AI is to find the player as efficiently as possible, but also to follow some predetermined patterns defined by the developers to ensure that the player can outsmart it. Hello Neighbor adds a very creative element to its AI by having it learn from the players' past actions and tries...

AI Controller

Let's discuss what the main difference is between a Player Controller and an AI Controller. Both of these actors derive from the base Controller class, and a Controller is used to take control of a Pawn or Character in order to control the actions of said pawn or character.

While a Player Controller relies on the input of an actual player, an AI Controller applies AI to the characters they possess and responds to the environment based on the rules set forth by the AI. By doing so, the AI can make intelligent decisions in response to the player and other external factors, without the actual player explicitly telling it to do so. Multiple instances of the same AI pawn can share the same AI Controller, and the same AI Controller can be used across different AI pawn classes. AI, like all actors inside Unreal Engine 4, are spawned through the UWorld class.

Note

You will be learning more about the UWorld class in Chapter 14, Spawning the Player Projectile, but...

Recasting the Nav Mesh

When you added NavMeshBoundsVolume, you may have noticed that another actor was created automatically: a RecastNavMesh actor called RecastNavMesh-Default. This RecastNavMesh acts as the "brain" of the Nav Mesh because it contains the parameters needed to adjust the Nav Mesh that directly influences how the AI navigates the given area.

The following screenshot shows this asset, as seen from the World Outliner tab:

Figure 13.4: The RecastNavMesh actor, as seen from the World Outliner tab

Note

There are a lot of parameters that exist in RecastNavMesh, and we will only be covering the important parameters in this book. For more information, check out https://docs.unrealengine.com/en-US/API/Runtime/NavigationSystem/NavMesh/ARecastNavMesh/index.html.

There are only two primary sections that are important to you right now:

  1. Display: The Display section, as the name suggests, only contains parameters that affect the...

Behavior Trees and Blackboards

Behavior trees and Blackboards work together to allow our AI to follow different logical paths and make decisions based on a variety of conditions and variables.

A behavior tree (BT) is a visual scripting tool that allows you to tell a pawn what to do based on certain factors and parameters. For example, a BT can tell an AI to move to a certain location based on whether the AI can see the player.

To give an example of how BTs and Blackboards are used in games, let's look at the game Gears of War 5, which was developed with Unreal Engine 4. The AI in Gears of War 5, and throughout the Gears of War series, always try to flank the player, or force the player out of cover. In order to do this, a key component of the AI logic is to know who the player is, and where the player is. A reference variable to the player, and a location vector to store the location of the player, exist in the Blackboard. The logic that determines how these variables are...

Vector Transformation

Before you jump into the next exercise, it is important that you get to know about Vector Transformation and, more importantly, what the Transform Location function does. When it comes to an actor's location, there are two ways of thinking of its position: in terms of world space and local space. An actor's position in world space is its location relative to the world itself; in more simple terms, this is the location where you place the actual actor into the level. An actor's local position is its location relative to either itself or a parent actor.

Let's consider the BP_AIPoints actor as an example of what world space and local space are. Each of the locations of the Points array are local-space Vectors because they are positions relative to the world-space position of the BP_AIPoints actor itself. The following screenshot shows the list of Vectors in the Points array, as shown in the previous exercise. These values are positions relative...

Player Projectile

For the last section of this chapter, you will focus on creating the base of the player projectile, which can be used to destroy enemies. The goal is to create the appropriate actor class, introduce the required collision and projectile movement components to the class, and set up the necessary parameters for the projectile's motion behavior.

For the sake of simplicity, the player projectile will not use gravity, will destroy enemies with one hit, and the projectile itself will be destroyed on hitting any surface; it will not bounce off walls, for example. The primary goal of the player projectile is to have a projectile that the player can spawn and use to destroy enemies throughout the level. In this chapter, you will set up the basic framework functionality, while in Chapter 14, Spawning the Player Projectile, you will add sound and visual effects. Let's get started by creating the player projectile class.

Exercise 13.11: Creating the Player Projectile...

Summary

In this chapter, you learned how to use the different aspects of the AI tools offered by Unreal Engine 4, including Blackboards, behavior trees, and AI Controllers. With a combination of both custom created Tasks and default Tasks provided by Unreal Engine 4, and with a Decorator, you were able to have the enemy AI navigate within the bounds of the Nav Mesh you added to your own level.

On top of this, you created a new Blueprint actor that allows you to add patrol points with the use of a Vector array variable. You then added a new function to this actor that selects one of these points at random, converts its location from local space into world space, and then returns this new value for use by the enemy character.

With the ability to randomly select a Patrol Point, you updated the custom BTTask_FindLocation task to find and move to the selected Patrol Point, allowing the enemy to move from each Patrol Point at random. This brought the enemy AI character to a whole...

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 2020Publisher: PacktISBN-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.
undefined
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 €14.99/month. Cancel anytime

Authors (4)

author image
Hammad Fozi

Hammad Fozi comes from a gaming background and has been extensively working on Unreal Engine since 2017. He has been part of some very successful AAA projects such as Virtua FanCave (and Metaverse), Unnamed AAA Sci-Fi DJ Experience, Heroes and Generals, and Creed: Rise to Glory VR. Hammad has worked with teams who have had experience working at Ubisoft, Warner Bros. Games, 2K Games, and more! He has successfully helped teams consisting of 10–30 people to scale to 150+ in size over his very short yet impressive career. Hammad currently works as a senior C++ game developer and has extensive experience in working with VR and augmented reality, PC/PS5/Xbox/Android/iOS/macOS game development, and Web3/Metaverse/NFT systems (within Unreal Engine).
Read more about Hammad Fozi

author image
Gonçalo Marques

Gonçalo Marques has been an active gamer since the age of 6. He has been using Unreal Engine since 2016 and has done freelance and consulting work using the engine. Gonçalo also released a free and open source plugin called UI Navigation, which has garnered an extremely positive reception with over 100,000 downloads and is still receiving frequent updates and fixes. Thanks to the development of this plugin, he became an Epic MegaGrant recipient. He is now working at Funcom ZPX, a game studio in Lisbon that has contributed to games such as Conan Exiles, Mutant Year Zero, and Moons of Madness. Gonçalo is currently working on a new Funcom game in the Dune universe.
Read more about Gonçalo Marques

author image
David Pereira

David Pereira started making games in 1998 when he learned how to use Clickteam's The Games Factory. He graduated in computer science from FCT-UNL, where he learned about C++, OpenGL, and DirectX, which allowed him to create more complex games. After working in IT consulting for a few years, he joined Miniclip in Portugal where he worked on popular mobile games such as 8 Ball Pool, Gravity Guy 1 and Gravity Guy 2, Extreme Skater, iStunt2, Hambo, and many others. Since then, he has been the lead developer for MPC in the John Lewis Christmas VR Experience, worked on an earlier version of Mortal Shell, and did volunteer work teaching people with Asperger's how to make games with Unreal Engine 4. Today, he's working on his own game, a soon-to-be-announced first-person action RPG.
Read more about David Pereira

author image
Devin Sherry

Devin Sherry is originally from Levittown, NY, located on Long Island. He studied the topics of Game Development and Game Design at the University of Advancing Technology where he had earned his Bachelor of Arts in Game Design in 2012. During his time in college, Devin worked as a game and level designer with a group of students called Autonomous Games on a real-time strategy styled, third-person shooter called The Afflicted using Unreal Engine 3/UDK where it was presented at GDC in 2013 at the GDC Play Showcase. Today, Devin works as an independent game developer located in Tempe, Arizona, where he works on personal and contracted projects. His achievements include the title Radial Impact, which can be found in the Community Contributions section of the Learn Tab of Unreal Engine 4's Launcher, and his work on his YouTube Channel, Devin Level Design, where he educates viewers on game development within Unreal Engine 3, UDK, and Unreal Engine 4.
Read more about Devin Sherry