Join Alan Thorn for an in-depth discussion in this video Finite state machines, part of Cert Prep: Unity Particle Systems, AI, and Audio.
- [Narrator] In this chapter, we're going to take a look at artificial intelligence, sometimes known as AI. Effectively, this refers to the brain of an NPC character, it's ability to make intelligent decisions inside the game. In the preceding chapter we saw a particular branch of AI that was path finding. By using this system, we can build a navigation mesh representing the floor of the level, and it would allow an NPC character a Nav Mesh agent to make intelligent decisions about how to navigate around the environment to reach the player character.
But of course being able to travel from the point where the NPC is standing and reaching the player character is only part of the equation when it comes to making sensible decisions for an NPC character. In the case of this setup here, and I'm just going to maximize the view, but in the case of this setup here, I want to create an enemy character that is going to pose a danger for the player. Now to actually pose a danger, we're going to need intelligence beyond simple pathfinding.
We do want to use the pathfinding features so that the NPC character can move around the level, but there are some additional behaviors that we want to consider. In particular for the NPC character here in the viewpoint, I'm going to have primarily three different behaviors. There's going to be the idol behavior, and effectively that's going to take effect when the character is walking around the level and then looking for the player character. They haven't found the player. They're simply looking around to see if the player can be seen.
That's the idol state. They're going to pick random destinations in the environment to move to. They're going to move there and, along the way, they're going to figure out if they can see the player. Now if the NPC does end up being able to see or to sight or to spot the player character, then the NPC should change its behavior pattern. It should stop wandering idly around the level and instead to change to a chase state. That is it should chase after the player character with the aim of trying to catch up and move near enough to the player character.
Then, when the NPC character does move near enough to the player, it should then proceed to attack them to inflict damage upon them. So here in this simple scenario, we have fundamentally three different states for the NPC character, and this kind of state is known as an FSM, which simply stands for finite state machine. There are a finite number of different states that the NPC character can be in. It can be in only one of those states at any one time.
It could be idle, chase, or attack, but it can't be in two at the same time. It can only be in one of them at any one time. And in this case, the NPC character is going to begin in the idle state. Now offscreen I've gone ahead and actually coded the artificial intelligence for this NPC character, and what I want to focus on in this movie and the remaining movies in this chapter, is exactly how that code works. You don't need to see me type every single line character by character. Instead the code is created, and I'm going to explain how it operates.
You can see here that I'm selecting the NPC character and, in the project panel, we have an AI agent script that has been attached to the NPC character. If I take a look inside the Object Inspector here, you can see that AI agent script has been attached and this represents the intelligence of the NPC character. Now you can see here we have a Current State field representing the current state of the NPC and by default this is set to the idle state, that is the wandering around state.
I can click in the drop-down box to change the state to chase or attack, but by default, the NPC should begin in idle. Now we also have a check mark field here that is Can See Player and when the NPC can see the player, this is simply going to be enabled. We also have the viewing angle to control how far out it is that the NPC can see the player character. And then finally we have the Attack Distance representing the distance in meters that the NPC has to be away from the player character before they start attacking them.
Now in addition to this, let's take a look at how this NPC character is also set up here inside the scene beyond the AI agent script. You can see how that I have tagged this character as an enemy character, and if I scroll down, you'll see that it has a Nav Mesh Agent component attached to allow it to move around the level using the navigation mesh. If I switch to the Navigation Mesh tab, you can see that the floor mesh for the navigation mesh has already been generated allowing the NPC to move around the level.
In addition to this, if I select the NPC, you can see that it also has a Box Collider component marked as a trigger, and this is extending outwards from the front of the character and effectively represents the area inside which the NPC can see. So for example, if the player enters this boxed area, then it means that the NPC character could potentially see the player. Let me just press play on the toolbar to demonstrate how this scene works here. So I'm going to press the play button on the toolbar and straightaway the artificial intelligence kicks in.
You can see that we're in the idle state. We're choosing a random point on the navigation mesh for the NPC character to move to. So right now it's picking different points. It's now picking a point over here, moving there, and actually if I grab the player character and I move it into an area I found I don't need to, because right now the NPC has finally found the player, and it's reached near enough that it has entered the attack state and remains there while it's attacking the player. So that's a pretty good setup here.
We've created some artificial intelligence for an NPC character. In the next movie, we're going to open up the AI Agent class and look at how this intelligence is implemented.
This course is part of our Unity cert prep series, focusing on exam objectives related to particle systems, artificial intelligence (AI), navigation and pathfinding systems, and audio. Instructor Alan Thorn teaches you how to control particle emitters, including particle life and size. He explains how to use navigation meshes to control obstacle avoidance, create compelling NPCs by setting up and triggering animation states, and add music and sounds effects to your 2D and 3D games.
Find out more about Unity Certified Developer exam at https://certification.unity.com/.
- Shaping and controlling particle emitters
- Adjusting particle life and size
- Moving objects
- Working with navigation meshes
- Starting and completing idle states
- Starting chase and attack states
- Adding music and 2D and 3D sound