Using our new AIController class
Ever notice how one player can be any character they desire? This is the hierarchy that creates the pawn and the controller. The controller is what the player inherits after waiting for some time in the game lobby. It is used to manage the input and connection from the player. This class comes with additional functions to help navigate the bot and the ability to assign a Behavior Tree to the controller. In this demonstration, we will cover some of the basics of the AIController class.
Assigning the AIController class
So, now that we have what we need to create an AI, we will assign the MyController
class to the MyCharacter
base. To do so, go to the Defaults section within the MyCharacter
blueprint. Search for AIController Class and set it to MyController
, as shown in the following screenshot:
When a character isn't possessed, it will automatically be possessed by AIController. So, with the change we just made, the default AIController class that possesses our MyCharacter
blueprint will be MyController
.
Placing the pawn
It's assumed you understand such a simple task! Let's place our newly set up pawn into the bright beautiful world by dragging and dropping it from Content Browser:
Drag and drop MyCharacter blueprint to the level to create a pawn
Note
There is a long line of people who have lost work to crashes and other mishaps; if you would like to join them, please skip this step. Otherwise, navigate to File | Save All whenever you make some significant new changes to the level.
Sending the instructions
Now that we have our project set up and saved—or not saved for our brave fellows—let's move forward to the fun part: blueprints! Let's take a look at the steps to send the instructions:
- Open your
MyController
blueprint within Content Browser and zoom in to the EventGraph:So, the plan is to have our bot move around randomly. This will be built in a simple fashion, so we will do the computing by hand.
- First, let's create an Event Tick node that will be triggered in every frame as the game runs.
- We will add a Delay node to receive signals from Event Tick to set the Duration pin to 1.
- Next, we will add a Move To Location node, which will signal the
AIController
class to tell its pawn to move to the destination point specified. - As we didn't use Path Finding at this time, which is a subject we will touch upon later, go to the MoveToLocation node and set bUsePathFinding to false or leave it unselected.
The EventGraph for your
MyController
class should look similar to the following screenshot: - With the MoveToLocation node ready, we now need to supply it with a random location. We will grab the current location of the controlled pawn, create an additional vector with a random value of -255 to 255 for the X and Y variables, leaving Z to 0.0. Then, we will add the location from the controlled pawn to the vector we just created. You should have a blueprint similar to the following setup:
- Let's finalize this and move this new random location blueprint to the MoveToLocation node that we set up previously. Now, let's connect the results of the addition of the two vectors to the destination of the MoveToLocation node. With this done, you should have a blueprint setup similar to the following preview:
Save it all!
Small tips on MoveToLocation
Here are some basic tips on MoveToLocation:
- AcceptanceRadius: This allows you to increase the radius that is acceptable for a completed move. Let's suppose that there is an enemy holding a sword who wants to attack the player. The AcceptanceRadius option will help you define how far this enemy should be standing away from his target—ideally 1 meter—and then perform an attack animation to swing the sword.
- bStopOnOverlap: This tells your bot to stop if it overlaps the point rather than going precisely there. This will take into consideration the radius of the collision mesh attached to the bot.
- bUsePathFinding: If this is selected, the bot will use the NavMesh option to find its destination. If unselected, the bot will simply move in a straight line to the destination, not taking into account any obstacles. This helps save the performance in some situations.
- bProjectDestinationToNavigation: This projects the location on the navigation data before using it. This helps validate the target actor's location—that is, whether it exists on a playable area or not.
- bCanStrafe: This determines whether the AI can transverse diagonally on NavMesh or not.
- FilterClass: This allows you to use AreaClass, which is another navigation component that affects the navigation of the AI. This effects changes such as exclusion or exclusive access to areas of NavMesh and alters the navigation cost.