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:

  1. Right-click and select Blueprint.
  2. At the lower end of the window, let's drop all the classes and search for AIController.
  3. Select AIController under Controller and hit Select in the lower-right corner.
  4. We will name this AIController Enemy.
  5. 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:

  1. Right-click in an empty area within EventGraph and search for Event Begin Play.
  2. Pull from the exec pin and search for Get All Actors Of Class.
  3. Set the Actor Class pin to Hero.
  4. Pull from the Out Actors array and search for ForEachLoopWithBreak:
    Adding the Enemy AI

We want a filter for the Hero class within the MyCharacter pawn returned. To do so, follow these steps:

  1. Pull from the Array Element pin and search for Cast to MyCharacter.
  2. Then, pull from the As My Character pin and search for Get Controller.
  3. Get the class of the controller using the Get Class node.
  4. Compare the Hero class with the Class = Class node.
  5. Pull from the return of Equal and create a Branch node.
  6. From the Loop Body pin, link the newly created branch.
  7. From the True exec pin of the Branch node, create the SET node.
  8. Then, pull from the pure cast to the SET node we just created.
  9. We want to comment this and call this section Find Hero:
    Adding the Enemy AI

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:

  1. Right-click on the EventGraph section and search for Event Tick.
  2. From the exec pin, drop and search for Delay.
  3. Set the Duration pin to .05 seconds.
  4. Pull from Completed and create a new node Move to Location:
    Adding the Enemy AI

Now, we need to get the direction from Enemy to Hero and move Enemy in this direction. We can do this thus:

  1. Get the Hero variable and drop it near the Delay node.
  2. From the Hero variable, get the actor's location.
  3. Pull from Return Value of the GetActorLocation node, drop it, and search for Get Direction Vector.
  4. Now, right-click and search for Get Controlled Pawn.
  5. Pull from the Return Value pin of Get Controlled Pawn and GetActorLocation.
  6. Then, pull from the Return Value pin of the GetActorLocation node and plug it into the From pin in the Get Direction Vector node.
  7. Pull from the Return Value pin of the Get Direction Vector node and Make Rot from X.
  8. Pull from Make Rot From X and Get Forward Vector.
  9. Now, from Get Forward Vector, we will multiply it by 255 to get 255 units in the forward vector direction.
  10. Lastly, we will add this to the GetActorLocation node of the Hero pawn.
  11. The result of the addition is the destination for Move to Location:
    Adding the Enemy AI
  12. 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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset