< Return to Video

Stealth game tutorial - 401 - Enemy Setup - Unity Official Tutorials

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

more » « less
Duration:
07:42

English subtitles

Revisions