♪♪♪
Zak Parrish: All right, we are back.
We are ready to start building some of the actual gameplay in our game.
We will begin by creating an Enemy Character that we can interact with.
In the Content Browser under C++Classes, open the TwinStickShooter folder.
Here is our BaseCharacter C++ class.
Right-click on the BaseCharacter and
select Create Blueprint class based on BaseCharacter.
Call this class "EnemyCharacter".
These are such creative names, right?
For Path, select the Blueprints folder.
Click the Create Blueprint Class button.
Wait a second, and it magically appears.
Dock this window at the top and adjust the panels
so we can see all the different bits and pieces again.
We have some of the same things to do as we did with the Hero Character.
Select the Mesh Component. In the Details panel,
click the Mesh drop down menu and select SK_Mannequin (Skeletal Mesh).
Place the Skeletal Mesh inside the capsule just like before.
Press E to rotate the mesh 90° so it is facing forward.
Now, I don't want the enemies to be the same color as the hero.
That is going to be be confusing and too much to look at.
Let's figure out how we can change the color.
In the Details panel, Double-click to open the Skeletal Mesh.
This opens the Skeletal Mesh on the Mesh tab in Persona.
We can pan around, and see how awesome this character looks.
He does look pretty cool, but we can also see the materials applied to him.
There are two (2) materials: M_UE4Man_Body and UE4Man_Chest_Logo.
The M_UE4Man_Chest_Logo should be an instance of M_UE4Man_Body.
Double-click to open M_UE4Man_Body in the Material Editor.
There is not that much going on here.
It is really just blending together a few layered materials.
You have some areas that look like metal, dark grey plastic, and then this beige
plastic which sort of dominates the whole character.
That beige color is a parameter named BodyColor.
If we make a dynamic version of this material and change BodyColor,
we can thereby change the color of the entire character.
Note the spelling, it is BodyColor without a space.
I am going to select BodyColor inside the Details panel
and press Ctrl + C to Copy, so I don't have to type the name later.
Go back to EnemyCharacter and go to the Construction Script.
Drag-and-drop the Mesh Component into the script to create a reference to it.
Drag a wire off Mesh.
We need a dynamic version of the material that is applied to Index 0.
How do I know the material is at Index 0? Go to EnemyCharacter.
In the Mesh Details panel, we have Element 0 and Element 1.
Element 0 is the main material. Element 1 is the chest logo,
which is just giving us the little UE4 logo you see here.
I can zoom right up on the logo.
Go back to EnemyCharacter.
Drag a wire off Mesh and type "dynamic".
Select Create Material Dynamic Instance.
This is a kind of a do it all at once node. I love this node.
This node is grabbing whatever material found in Element Index 0,
and it is converting it to a dynamic version
so you can change any parameters found inside of it.
Once it has done that, it reapplies it back to the same Element number.
Connect the Construction Script to the Create Dynamic Material Instance.
Let's set this to a value.
I am not saying we will ever use it, but let's do it anyway.
Right-click the Return Value and select Promote to Variable.
Name this variable "DynaMat".
Drag a wire off the Return Value of the
Dyna Mat variable, type "set vector"
and select Set Vector Parameter Value.
This node asks for the Parameter Name you want to talk to.
Remember from earlier, we copied BodyColor.
Press Ctrl + V to Paste BodyColor into Parameter Name.
What color are we going to set it to? Click Value to open the Color Picker.
This is where you decide what color you want your Enemy to be.
Let's choose some shade of red.
Let's choose a bright red. There you go.
It just screams danger.
Go to the Construction Script and click Compile. Go to the Viewport tab.
The character is bright red, which is pretty cool. Go to the Construction Script.
Right-click on the Value pin and select Promote to Variable.
Call this variable "EnemyColor".
Go to the Viewport tab.
In the My Blueprint panel, select EnemyColor.
Click Compile. Go to the Details panel under Default Value,
Double-click Enemy Color to open the Color Picker, and set this to any shade you want.
Your enemy can be anything.
He could be bright yellow.
Or, he could be orange.
I am going to set him back to red, and we will leave that setting.
I will desaturate the color just a little bit. There we go.
That looks very dangerous. You will notice
this does not change the color of the logo. That is fine.
We will leave it as a nice little beige logo on his chest.
Now, we have a nice visual differentiation between our Hero and our Enemy.
That should make it easier to tell them apart on the screen, which is great.
With that done, let's set up the AIController that is going to drive this Enemy around.
I am only doing this because we are only about 6 minutes into this video.
It is such a simple concept that I didn't want to put it in
its own video because it would be very, very short.
Go to the Content Browser and select the Blueprints folder to set this up.
Right-click inside the Blueprints folder and select Blueprint Class.
We want to base this class off AIController,
which does not appear in our Pick Parent Class list. To find AIController,
expand All Classes, type and select "AIController".
Click the green Select button.
Name this class EnemyAI.
Double-click to open EnemyAI.
This Blueprint is like any other Blueprint.
It has all of the same panels. I will put back the panels back like they were.
Go to the Event Graph.
The AIController has some components to help it do some things.
It has a TransformComponent.
It has a PathFollowingComponent, which will make use of a Nav Mesh.
It also has an ActionsComp or Actions Component,
which is going to be used for various AI behaviors.
As I mentioned, this is going to be a very, very simple AIController.
Right-click in the graph, type and select "Custom Event".
Call this event "TrackPlayer".
That is all it is going to do.
You could call the event ChasePlayer, TrackPlayer, HuntPlayer, or whatever.
It is going to do the same thing.
It is going to do a special function. Drag off TrackPlayer,
type and select "AI Move To".
The AI Move To node takes a Pawn
and tells it to move toward a destination, or toward a
Target Actor. Once it gets to a within the acceptance radius, it stops.
The nice thing about the AI Move To node is
it will keep moving the Pawn toward that target
Actor until it hits the acceptance radius.
This means we don't have to call this AI Move To node very often
in order to have something chase our player pretty much all the time.
We need to assign the Move To node the Pawn that is being driven,
and we need to assign a Target.
The Pawn that is being driven is easy to assign.
Since this is the AIController, this Controller will be plugged
into the side of our Enemy Character's head like a cartridge.
That is a metaphor. That is not how it is really going to be. There is no cartridge
hanging off the side of its head. Go with me.
Right-click in the graph, type and select "Get Controlled Pawn".
The Get Controlled Pawn node is literally a reference to the Pawn
controlled by this AIController.
Next, we need the Target Actor.
Right-click in the graph, type and select "Get Player Character".
There it is. We get the Player Character.
Plug the Return Value into Target Actor. The Player Character becomes the Target Actor.
That is all we need to do.
We just need to use the Get Player Character node.
Now when TrackPlayer is called, the Enemy will just
move toward the Hero until it gets within 5 units.
A value of 5 units is fine; we don't need to change that.
However, we do need to have something that
keeps calling TrackPlayer over and over.
We want something to keep pinging to keep tracking the player
because if the Enemy ever gets within this radius,
the AI Move To node will stop firing.
The Enemy will never chase the player again if we don't explicitly tell him to.
To do that, we are going to set a timer. Right-click, type and select "Set Timer".
A timer in UE4 is just like a camera timer; there will be a countdown.
At the end of the countdown, the timer will call some function.
Type the name of the function inside Function Name.
Set Function Name to "TrackPlayer".
How often do we want to track it?
Set Time to 1.0 second.
Check the Looping checkbox; it will call TrackPlayer every second.
Notice, Set Timer is not an event.
We need something that will plug into the executable pin and call this node.
Drag the Event BeginPlay node over and plug it into the Set Timer node.
This means the moment this AI character spawns, or the moment the game begins,
this Set Timer will be called.
Set Timer is going to keep calling TrackPlayer once per second
and keep telling the AI to chase the Player, over and over.
Click Compile and click Save.
Then, go to the EnemyCharacter tab. We are going to set up a few things.
Click the Class Defaults button.
In the Details panel under Pawn, click the AIController Class property
drop down menu and select EnemyAI.
Click Compile and click Save.
Now, watch this. Drag-and-drop EnemyCharacter into the world and click Play.
Nothing is going to happen yet.
Let's just move over to the enemy. He is just sitting here being boring.
His behavior is actually there.
He has the requisite logic to make him move around.
He doesn't have a Navigation Mesh yet.
Navigation Meshes are very easy to do in UE4.
Watch this. In the Modes panel, select the Volumes category.
You should see Nav Mesh Bounds Volume.
Drag-and-drop Nav Mesh Bounds Volume onto your floor.
Move the Nav Mesh Bounds Volume down because it needs to penetrate into your floor.
Press R to use the Scale tool and scale
the volume so it completely covers the floor.
Press the P key to visualize your Nav Mesh.
There you go.
There is your Navigation Mesh. It is already done.
Again, it is very, very easy. Click Play.
Oh my goodness, he is so fast! We can't get away from him!
That is actually kind of ridiculous.
We need to slow him down.
Click Pause to stop playing.
In the Content Browser, Double-click to open the EnemyCharacter.
In the Components panel, select the CharacterMovementComponent.
In the Details panel under Character Movement: Walking,
set the Max Walk Speed to 200.
That will probably make the game too easy, but you can always tweak the value later.
Click Compile and click Save. Go to ArenaMap and click Play.
There you go. The enemy is chasing us,
and he will keep chasing us over and over.
If he touches us, he will wait a second
and then he will start chasing again.
There you go.
We have an enemy with his own AI that is just
chasing the Player, which is pretty awesome.
Press Pause.
That is going to wrap things up for this video.
Thanks a lot.
♪♪♪