Categories
Uncategorised

GD2 Blog: Week 8

Unreal AI

As of November 22nd, I looked at some of the AI systems within Unreal. This includes the use of the Blackboard, AI controller and Behaviour Tree to make a simple NPC that patrols randomly and chases the player once it sees them.

Before starting, I created a new blank character that started off as a blank capsule. Looking around in the properties of the new character I set it a new skeletal mesh and reused the same animation class as the third person character (See Figure 1).

Figure 1

To start, I created a Blackboard blueprint which would contain all the variables that would be used within the Behaviour Tree later on.

The Blackboard contains several user defined Keys that hold information used by the Behavior Tree to make decisions.

docs.unrealengine, 2021

The Blackboard contained an Object reference key to the Actor itself, a boolean activated line of sight and a vector based patrol location which would hold a randomized set of coordinates for the AI to walk towards (See Figure 2).

Figure 2

Next up was to set the Behaviour Tree, this would contain a queue of instructions for our AI to follow in a certain order depending on their condition.

Behavior Trees assets in Unreal Engine 4 (UE4) can be used to create artificial intelligence (AI) for non-player characters in your projects.

docs.unrealengine, 2021

Here is the behaviour tree, it first checks if it has a line of sight on the player and initiates a chase sequence using Tasks. If the player is not within their sights then they initiate a patrol sequence (See Figure 3).

Figure 3

For the NPC, there will be different types of movement speeds initiated by the blueprint depending on whether they are chasing or patrolling. I created a function which simply gets the NPC’s walk speed and changes their max walking speed to the new one (See Figure 4).

Figure 4

This is then used within the Tasks of the Behaviour Tree, for instance once the chase sequence is initiated, a cast is done towards the NPC where it targets their character movement and sets it as the new chase speed.

Figure 5

For the patrol movement, it gets the actor location and finds a random point within a certain radius to set as their next patrol location. It also changes the player’s speed to a patrol one which is more slower than the chasing speed (See Figure 6).

Figure 6

Next up we have an AI controller to set our NPC to, this can help observe the game world using features such as perception to look for players. In our case, we will be using the perception of the NPC to activate a chase sequence if it currently sees the player.

The job of the AIController is to observe the world around it and make decisions and react accordingly without explicit input from a human player.

docs.unrealengine, 2021

To set up our AI’s perception, we will be using an AI Sight config where we can set their sight radius and what they’re able to detect (See Figure 7).

Figure 7

Once set up, we can have an event that activates once the AI sees our player (At this point, the player has been given a tag to help the AI identify them)

Once triggered, our event will enable the blackboard keys that make the AI to chase the player. In it is also included a timer, so if the AI loses sight of the player for a certain amount of time then these keys will be disabled and make the AI stop chasing them (See Figure 8).

Figure 8

I managed to get this working in-game, Figure 9 shows the AI patrolling if they don’t have sight of the player.

Figure 9

Once seen, their chase sequence is activated. They will pursue the player until they lose sight of them for a certain amount of time as shown on Figure 10.

Figure 10

I feel as if AI is interesting to work with in Unreal, although I have briefly touched upon it using C++ which is what I might need to study more on. However I feel confident in using blueprints to set up simple AI quickly, which may be handy for prototyping in general.

Bibliography

Docs.unrealengine.com. 2021. AIController. [online] Available at: https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/Framework/Controller/AIController/ [Accessed 29 November 2021].

Docs.unrealengine.com. 2021. Behavior Trees. [online] Available at: https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/ArtificialIntelligence/BehaviorTrees/ [Accessed 29 November 2021].

Leave a Reply

Your email address will not be published. Required fields are marked *