In this assignment we will setup the enemies in the game.
Each enemy will be on a set patrol route by default.
If he is made aware of the player's location
by seeing or hearing the player,
by the player triggering an alarm
system or by another enemy spotting
the player then he should rush to the position
of the sighting or the triggering alarm.
If however the enemy can see and has
line of sight to the player he should
instead shoot the player
There will be two variables for storing the
player's location. One global variable
that all the enemies will be sent to and one
personal variable that only the
specific enemy will be sent to.
When the player is spotted, either by an enemy
or an alarm system
this will set the global variable.
which has already been created in our
LastPlayerSighting script.
When the player is heard by an enemy
either by running or using the attract attention feature
only the personal variable will be set.
If the global variable is not set
to the reset value indicating that the
player has been spotted then the enemies
should set their personal variable to
match the global variable's value.
and converge on the sighting position.
The enemies will have two colliders.
A large sphere trigger collider
and a capsule collider.
The sphere will be used as the enemy's
range of sight and hearing.
The capsule will be used for normal collisions.
Because the enemies will be moving will colliders
they will also need a rigidbody component.
This will be kinematic because the enemy's
should not react to collisions and the movement
will be dictated by the nav mesh.
As such, the enemies will also need
nav mesh agent component. The nav mesh
agent will physically move the enemy character
and this will provide information that we
can feed in to the animator controller
to define which animation should be played
to match this movement.
The enemies will be using a total of five
scripts, four of which will be applied
as components to the enemy and one
which will be a helper class.
The scripts will be as follows.
Enemy Sight.
This will work as the enemy's ears and eyes.
This script will be used to detect when the player's
within the sphere trigger collider.
Animator Setup.
This script will be a helper class that is
not applied to the enemy as a component.
Instead an instance of the class will be created
in another script so that the function
from it can be used. This will perform the
task of sending information from the Enemy
Animation script to the animator controller.
This will take information about direction and
velocity from the nav mesh agent
and manipulate it in to a form that
can be used by the Animator Setups script
to apply to the animator controller.
Enemy AI.
This works as the enemy's brain
and controls the way it behaves based on whether
the player is in sight or not.
It then feeds the information to the nav mesh agent
to determine what the enemy should do.
Enemy Shooting.
This controls the actions that are performed when
the enemy is shooting the player, including
the amount of damage and the rate of fire.
The first thing to do when creating an enemy
is to drag the Enemy FPX in to the Scene.
Grab char_robotGuard and drop it in.
We'll put him just outside the door of the second room
at (-18, 0, 6.5).
As with all dynamic game objects in the scene
we'll make sure it's using light probes.
Open the hierarchy and select the body
then check Use Light Probes.
We also need to do this for the helmet.
Locate it under the
skeleton - hips - spine - neck - head
part of the hierarchy.
This will mean that the robot is dynamically lit in the scene.
Before we add any components to it
let's tag the parent game object Enemy.
Select it from the Tag drop down.
Notice that because this asset was imported
with a rig we automatically have an animator
component that's waiting for a controller asset
which we will create later.
The first components we should add are the colliders.
We will start with a sphere collider.
We will need the centre of the collider to be
at the centre of the enemy, which is 1 in the Y axis.
The radius is the range of sight the enemy will have.
So 10 is a good starting point.
We can adjust this later if we need to.
Most importantly this collider needs
to be a trigger, so we'll check that box now.
Next the capsule collider.
We need to add this in addition to our sphere
collider so we will add it to a child
game object called char_robotGuard_body.
Again the centre needs to be moved up by 1.
But this time we only need the radius to be 0.3.
We also need to change the height to 2.
We will add the rest of the components to the root
game object, since we have colliders
that move we will add a rigidbody.
We need this to be kinematic so that the enemy
doesn't bounce when it hits something.
We now need the nav mesh agent.
Almost all of the starting parameters are fine
but we want to adjust the stopping distance to 0.8.
Now that we have the nav mesh agent on the enemy
this is a good time to bake our nav mesh.
In order for parts of the environment to be
included in the bake they should be marked
as Navigation Static.
EVerything that we marked as static when we
bake the light map will by default
also be navigation static.
But we can check whether this is the case by
seeing if the Static check box on
the top right in the Inspector is checked
and by looking in the drop down to the right
of this to ensure that navigation is included.
First open the env_stealth_static game object
so that the groups of game objects show.
We want the Debris - Ground - Lift Shaft - Props - both of the walls
to be navigation static.
We also want the battle bus and all of the
switch units to be navigation static.
Here we are simply checking that Navigation Static
is checked for each of these object.
It should already be.
Now open the Navigation panel.
You can do this through Window - Navigation
if you don't already have it open.
We have a couple of settings we want
to make sure of before we bake the nav mesh.
Under the Bake tab we want the
radius to be 0.3.
This will mean that enemies can fit
through slightly tighter spaces.
The height should be 1.
If this was too low then the nav mesh would go
under things like the battle bus causing
the enemies to try and run through it.
All that's left to do now is click Bake.
This should only take a few seconds.
That concludes the setup of the enemy.
So now is a good time to save the scene and
our project. In the next assignment we will create
the animator controller for the enemy.
Subtitles by the Amara.org community