Adding the Enemy AI
Let's go back to Unreal Engine and focus on Content Browser. We now need an opponent. To add this, follow these steps:
- Right-click and select Blueprint.
- At the lower end of the window, let's drop all the classes and search for AIController.
- Select AIController under Controller and hit Select in the lower-right corner.
- We will name this AIController
Enemy
. - Open
Enemy
AIController and go to the EventGraph section.
First, we must find Hero and then store it in a local variable to be used at any time. To do so, follow these steps:
- Right-click in an empty area within EventGraph and search for Event Begin Play.
- Pull from the exec pin and search for Get All Actors Of Class.
- Set the Actor Class pin to Hero.
- Pull from the Out Actors array and search for ForEachLoopWithBreak:
We want a filter for the Hero
class within the MyCharacter
pawn returned. To do so, follow these steps:
- Pull from the Array Element pin and search for Cast to MyCharacter.
- Then, pull from the As My Character pin and search for Get Controller.
- Get the class of the controller using the Get Class node.
- Compare the
Hero
class with the Class = Class node. - Pull from the return of Equal and create a Branch node.
- From the Loop Body pin, link the newly created branch.
- From the True exec pin of the Branch node, create the SET node.
- Then, pull from the pure cast to the SET node we just created.
- We want to comment this and call this section
Find Hero
:
We now have to constantly update the Enemy instructions to moving toward the fleeing Hero character. Note that there are that nodes that can directly achieve this, such as Simple Move to Actor, AI MoveTo, and so on. We will implement similar behavior to give you a look at how this can be done under the hood. Perform the following steps:
- Right-click on the EventGraph section and search for Event Tick.
- From the exec pin, drop and search for Delay.
- Set the Duration pin to .05 seconds.
- Pull from Completed and create a new node Move to Location:
Now, we need to get the direction from Enemy to Hero and move Enemy in this direction. We can do this thus:
- Get the
Hero
variable and drop it near the Delay node. - From the
Hero
variable, get the actor's location. - Pull from Return Value of the GetActorLocation node, drop it, and search for Get Direction Vector.
- Now, right-click and search for Get Controlled Pawn.
- Pull from the Return Value pin of Get Controlled Pawn and GetActorLocation.
- Then, pull from the Return Value pin of the GetActorLocation node and plug it into the From pin in the Get Direction Vector node.
- Pull from the Return Value pin of the Get Direction Vector node and Make Rot from X.
- Pull from Make Rot From X and Get Forward Vector.
- Now, from Get Forward Vector, we will multiply it by 255 to get 255 units in the forward vector direction.
- Lastly, we will add this to the GetActorLocation node of the Hero pawn.
- The result of the addition is the destination for Move to Location:
- Now, place a comment around this and name it
Chase Hero
.
Save it all!
Now, head back to the Viewport section and hit Simulate. You should now see our Hero character is running forward and turning right when it detects an obstacle in its way. Our Enemy character is chasing fast behind our Hero character. Now, imagine if you were in the Hero character's place; you would be in complete fear!