OK. Hi everyone. Welcome back to this tutorial
OK, let's go back to the Unreal Engine 4 Editor
Let's review what we made in the last lesson first
We have implemented AI basic turning in the last lesson
Now our AI will turn to wherever player goes
This is a simple mechanism
In this lesson, we will create another mechanism
That is, when seeing the player drawing a sword
The AI is told to get ready to fight
This is rather simple
Now I find a problem
That is, as soon as our camera moves to our AI's body
It makes a collision
For example, flashing like this
This is due to the collision
Our camera arm is about five meters
so the body of AI will be captured
and there will be a collision
Now I want to modify this object
Sometimes our camera arm also collides with our player
We can modify it here
Click the Collision Presents here and set it to Custom
Then turn off the Camera
Similarly, select the Camera and set it as Not
Then let's find our AI and apply the same setting
Because our camera will collide with the objects
Just turn off the Camera and ignore it
Now let's have a try
Draw the sword
OK
Now there is no problem
There is no problem when the base role attacks
and when the camera goes back here
OK
OK, we have realized this simple function
Now let's implement another one
which is to create a Animation Blueprints for AI
so that it will have an animation just like the player
Otherwise it will be strange to keep it in this T-Pose
Now let's go back to the Content Browser
Find the Animation Blueprints of character
AI blueprint is copied from the character blueprint
in order to animate it
OK, let's open this Animation Blueprint
Put it here
Let's see which variables to apply
Actually we only need to use two variables
from the character's blueprint
So when we switch to the AI later
we only need to create two variables
The first is Equip
This boolean tells our AI whether the sword is equipped or not
The other is Defense
Because our AI also needs defense
OK, let's switch to our browser
Then let's copy
Click on the Copy here
Certainly, we can't follow the previous name
Let's name it as BPA_NegativeAI
Let's move this to the AI folder
If you don't have these in the left
You can click this switch here
Then move it into this AI
Then open the AI folder and find this
We can delete these because they read from the player
Now this blueprint is to be used on AI
so we can no longer call variables from the player
We need to call variables from the AI itself
cast to bp
Select this NegativeAI
Let's put it here and then connect it to here
OK. Then organize them
Then get equip
Then get defense
Get these two
OK, it's almost done
Let's put this Reset Attack Montage aside
OK
Then let's go back to the AI
Then apply the Animation Blueprint we created for it
We have named it BPA_NegativeAI
Let's go back
Now you can find that our AI can turn towards our player
and its static animation has also been done
Now we need to implement another function
That is to use a Blueprint Interface to communicate with AI
Tell the AI that
we are ready to fight
Now let's go back to the Content Browser
Let's create a new folder here
Name it BP_WorldSetting
Put all relevant blueprints in here
We may use it less frequent
Let's put the Blueprint Interface into it first
It's not created from the Blueprint Class here
We can find it in the Blueprints under Create Advanced Asset
Find Blueprint Interface
Name it with the BPI prefix
It's the abbreviation of Blueprint Interface
So I like to name it with the BPI prefix
Please remember this prefix, it's very important
because there are hundreds types of blueprint interfaces
With this prefix, we can find it directly
Let's name it ReadyToFight
After we open it, we can see a new function here
This new function is the event we call
Let's name it Ready
OK
Then let's compile
OK. Then let's focus on the player
Then how to use the Blueprint Interface we created on the player
First, click on the Class Setup here
In it, we can change the Parent Class
And customize these blueprint options
We need to find the interface
Then you can find that there is zero implemented interface
Click the Add here
Here are actually 49 options to select from
Search for bpi, the prefix we used in here
Select this Ready to Fight. Then it's added directly
OK
Let's compile it again
Use this Blueprint Interface
when the player drawing the sword
Go back to the Equip here
After the player drawing the sword
we're going to make a call here
Enter bpi and close the Context Sensitive
It is suffixed by Message when we conveying message
The Target is what we want to convey the message to
Get the AI variable that we have instanced and put it here
It's like telling it that we're ready
Let's go back to the AI, it has Class Setup option
Just like the character
we need to add blueprint interface
Add BPI Ready to Fight
OK
Then how to receive the message from the player
Search ready to fight here
Add an Event Ready
to receive the message from the player
Then tick this
then enter montage
Turn off Context Sensitive
Here is Montage Play
Then Get Anim Instance, Mesh
As for the Asset, let's go back to the base role
AI and player has the same sword-drawing animation
Let's get it and apply it on the AI
OK, the basic function is done
Let's compile it
After the player drawing the sword, the AI also draws the sword
But the AI draws rather slow
Go back to the player and add a Sequence here
Let it delay about 1 second
and then tell AI to draw the sword
Otherwise 2.5 seconds is a bit slow
Let's add a Delay. OK
Place these boolean value in front
and keep these ones here
Now let's have a try
OK, see, it moves faster
Now it's ready to fight
OK
Then what we need to realize is
for example, after the player drawing the sword
the AI will get ready to fight
the AI will start to follow our player
OK, let's start to produce
Let's return to the AI Behavior Tree
Turn off those redundant projects
Let's return to our AI behavior tree
It's here
We need to create a new bool key named ReadyToFight
I think you should know, as we have covered in previous lessons
that our AI Behavior Tree cannot read
the values in the character's blueprint
But the values in our character's blueprint can set
the values of Blackboard in Behavior Tree
You should know this
And the AI Behavior Tree can only call the values in Blackboard
OK, now let's return to our AI
We need to read the blackboard here
Similar to this
Get Blackboard
and then set the blackboard value as bool
Link bool with Montage Play
Then tick Bool Value
Then Create Literal Name
Name it as ReadyToFight
OK. Then
OK, finished
Then when this bool value is ticked
The AI will be told to fight
Then let's go back to the AI Behavior Tree
Here let's put these two in one event
Then let's create a new Sequence here
As I told you before, the function of Selector is
if the first one is selected, the second one won't
and vice versa
Now it's like choosing one from two
Judge whether the first thing can be executed
If it can be executed, then the second won't be executed
Execute the second when the first can't be executed
Then Add Decorator on this Composite Node
I may have said it before the last lesson
that the Decorator is used to judge the values
Then let's get the value of ReadyToFight first
If this value is true, we execute the following
If this value is false, we execute the stuff here
Here we can name it Focus
OK
These two are completely independent events
If this is true, the AI starts to move to our character
Use Move To
As for Move To, we may choose not only vectors
but also blueprints
We can directly select the corresponding blueprint to move to
But as you can see
we can't select the Target we created from here
So we need to return to the Blackboard
Turn on the Key Type in Target
And the base class should be Actor
OK, now we can select the Target here
OK, now let's try it
The player draws the sword
Now the AI starts to move
But it moves so fast
A bit too fast, we can modify it here
Set Max Walk Speed
Let's set it to 150
Then let's delay the play
Let's see how many seconds to set
It's duration is 2.6
Actually, I think 2 seconds is OK
Let's set it to 2
We have set the Max Walk Speed to 150
Let's set it to 125
No, set it to 150, since its body is big
Let's try again
OK
The player draws the sword
Then the AI
Why it doesn't move
OK, now it moves
But now it walks slower than before
Then you will find it doesn't move
after reaching to a certain place
That is a problem
Let's return to here
We didn't enlarge it before
Now we need to scale up the Nav mesh
Let's enlarge it so that to cover the entire map
Place it here
Move it to the center
Enlarge it to cover the entire map
Now let's try it again
OK
OK, now the AI follows the player to move
OK, we have completed this function in this lesson
In the next lesson, we will create this
when the AI moves near the player, it attacks and rolls
Just like the player
it can attack and defend
So in the next lesson we will mainly introduce
the attacks and rolls of the AI
OK, so that's all for this lesson
OK, see you in the next lesson