A Deeper Look at Enemies
Checked with version: 2018.1
The 3D Gamekit contains 3 pre-made enemies: Chomper, Spitter and Grenadier.
They are all controlled by the EnemyController script and each one has a specific behaviour attached to them (ChomperBehavior, SpitterBehavior and GrenadierBehavior).
Chomper is a melee enemy that will run towards the player when it spots them, and attacks when close enough.
Time to stop pursuit is how long (in seconds) the player has to get out of the Chomper's line of sight before it stops pursuing the Player and returns to its original spot.
Spitter will launch an exploding acid spit toward the Player when she is spotted. If the Player gets too close to them and they are not currently spitting, they will try run away.
Flee Distance: when running away, once they’re far away enough from the player, they stop fleeing and resume attacking.
Grenadier is a tougher, bigger enemy. It will launch bouncing grenades toward the Player if they are far away, punch them if they are in front and close or deploy their temporary shield in a damaging explosion if the Player is close but not in front of them.
They can only be damaged from the back by hitting their glowing core.
Melee Range: if the player gets closer than that of the Grenadier, Grenadier will either punch (if player is in front) or deploy shield.
Range: if the player is close to the Grenadier but not yet in melee range, the Grenadier will launch a bouncing grenade toward the player. If the player is spotted but not yet inside that range, the Grenadier will move to reach them.
All enemies have a Player Scanner property on their respective behaviour that is used to setup how they can spot the Player.
For an explanation of the settings, see the TargetScanner documentation.
They also all have a Damageable component that defines how much health they have and how they can be damaged (e.g. Grenadiers can only be damaged from the back).
The location for that component is:
Chomper and Spitter: on the root GameObject, just next to their Behavior scripts.
Grenadier: on a child GameObject of Grenadier called DamageablePart in the Hierarchy
Some of those settings can be visualized on the scene view when an Enemy is selected.
The blue circle represents the Player Scanner detection radius and angle. Player will only be spotted if they walk within that circle.
The small yellow wire sphere is the Player Scanner height offset (i.e. where the "eyes" of the scanner are). The system will check if the line of sight between this and the player is unobstructed.
The red circle is the damageable hittable angle. The Player must hit the enemy within this area to cause damage.
Adding a Chomper
Let’s add a Chomper to our scene;
Go to Assets > 3DGameKit > Prefabs > Characters > Enemies > Chomper
Drag the Chomper Prefab into the scene
Position the Chomper somewhere near Ellen so we can test quickly
We can see a very large blue area covering the scene. This is Chomper’s Detection Radius, which is the the area that he can see. If Ellen walks in this area, Chomper will attack. We can adjust this setting, with Chomper selected in the Hierarchy;
In the Inspector, locate the Chomper Behaviour script
In Detection Radius, change the value to 4. This can also be achieved by quickly scrolling through values, hovering over the word Detection Radius, and then clicking and dragging to the left to quickly reduce the value or right to increase the value.
- Press Play and run towards the Chomper to enter his Detection Radius
When you enter his Detection Radius, Chomper will spot Ellen and start his attack behaviour, however he will be running on the spot and unable to move.
This is because we have not told Chomper’s AI what surface is walkable. We use Unity’s NavMesh system to do this. Chomper’s AI is handled by the NavMesh Agent Component. This gives Chomper the ability to walk on surfaces that we set, avoid obstacles and other Chompers, Spitters and Grenadiers while navigating the world.
We do not need to change any of these settings because Chomper and the other enemies are already set up to behave and navigate in a specific way.
Let’s set up the ground to be walkable by enemies by adding a NavMesh Surface;
- In the Hierarchy select the Plane (the ground)
At the bottom of the Inspector, click Add Component
Search for ‘NavMeshSurface’
Hit enter or click on the result NavMeshSurface to add it to Plane
- In the NavMesh Surface script, set the Agent Type to Chomper
This will tell the surface which Agent can walk on it. Note that only Chomper appears in this list and there is no Spitter or Grenadier. This is because all enemies use Chomper’s Agent settings.
- For the Include Layers dropdown, first click Nothing
- Now in the Include Layers drop-down, select Environment and Vegetation
This is a quick way to deselect many layers and reselect the ones you wish.
- Click the Bake button
This will Bake a NavMesh in your scene and make most surfaces walkable for your enemies. We can see where our enemies can walk in the Scene View as it’s shown by the light blue highlight.
Any gaps in the NavMesh Surface (blue highlight areas) mean that the enemies will not be able to walk in those areas.
Here we can see gaps just before our walls. This means that enemies will not be able to walk through our walls or door that we have set up.
Let’s test our Chomper by pressing Play. Now we can run up to the Chomper and in turn Chomper will run toward us and attack. If you run slightly away from the Chomper, his NavMesh agent will use the NavMesh Surface to navigate the world to chase you.
If you add more Chompers or other enemies in the scene, they will use the same NavMesh Surface, there is no need to repeat this for new enemy additions.
If you would like to now add new parts to your level or change the position of objects in your scene, you will need to rebake your NavMesh Surface by going back to the Plane and clicking Bake in the NavMesh Surface script.
Adding A Spitter
- Add A Spitter prefab from 3DGamekit > Prefabs > Characters > Enemies > Spitter by dragging it into your scene.
If you look at the Inspector you will see that the Spitter is much like the Chomper.
The Spitter is a ranged class of enemy that will run from Ellen when she is too close by using its Fleeing Distance property to determine when it should run away before attempting to attack. It then uses the Range Weapon to fire acid at Ellen.
Tip: If the Fleeing Distance is larger than the Detection Area it will flee only when detected and can result in a continuous fleeing pattern.
Ideally, you would have them start farther away to allow them a better opportunity to attack and be more formidable against Ellen. If you want to make the Spitter to take the Environment into consideration then you will need to change the View Blocker Layer Mask from DoNotDraw to Environment.
In our scene we have added the Spitters in the higher area to give the Spitters an advantage over Ellen when she travels on the Moving Platform to make it more challenging for the player to take them down.
Play the scene after adding them to see their behaviours, and try different detection radius distances and fleeing distances.
Adding A Grenadier
Add A Grenadier from 3DGamekit > Prefabs > Characters > Enemies > Grenadier and drag it into the scene.
The Grenadier is a boss enemy and has more details to consider when working with it. When selecting it in the scene the damage radius is set to rear only to force the player to target its core.
The Behaviour script is very similar to Chomper and Spitter but combines melee and ranged with properties for it’s different features: Fist Weapon, Grenade Launcher and Shield.
To see the damageable component, expand the Grenadier in Hierarchy and find DamageablePart
This will show you the Damageable script and content relating to health, how long it is invulnerable and hit areas.
In the events area of the Damageable component there are functions attached to the Grenadier On Death, Display particles on Hit and resetting it’s vulnerability on hit.