Lesson 4: Meet your new enemies

Checked with version: 2018.2.1 - Difficulty: Beginner

In Lesson 4, you'll learn how to add and defeat the enemy characters, Spitter and Chomper.

Set yourself up for success by setting aside at least 1 hour to complete this lesson. If you get stuck, seek out answers and help in our moderated 2D Game Kit forum.

Enemies

First, let's build a good place to add Spitter. We're eventually going to squash Spitter by pushing a box onto him (sorry, little guy!), which is why we're building the level out like so:

  • Use the Tilemap feature to build out an area like shown in the image below, where Ellen can be placed higher up, and the Spitter can be placed on a platform underneath a drop, like this:

  • Once you've built the level, move Ellen to the top of the drop. Tip: To quickly locate anything in your Scene, select it in the Hierarchy and then move your cursor so it hovers over the Scene view and press F on your keyboard. Once you've located Ellen, in case you need a refresher, to move her, select Ellen in the Hierarchy, then press Ctrl (Windows)/Cmd (Mac) and click on Cameras. Then click and drag on the transform tool (the blue square) or the move tool (the red arrow) to place her at the top of the drop
  • Add Spitter by navigating to the Project window and go to Assets > 2DGameKit > Prefabs > Enemies
  • Click and drag SpitterPrefab into your Scene so he’s on the lower level next to the drop, as shown in the image above
  • Press Play to see how he attacks by launching balls of goop!
  • Exit Play mode and select SpitterPrefab in the Hierarchy. You'll notice a green circle appears around Spitter:

The green circle indicates Spitter's View Direction, View FOV (Field of View), and View Distance, which you can locate in the Inspector under Enemy Behavior (Script) > Scanning Settings as seen in the image below:

  • Change the View Direction to 30
  • Change the View Fov to 182
  • Change the View Distance to 6
Damaging with objects

In this section, we’ll explore the damage system. To do this, we’ll go through the steps of dropping a box on a Spitter to kill him. Poor fellow!

  • In the Project window > Prefabs > Interactables locate PushableBox and drag it into the scene as shown below:

We need to enable the PushableBox to be able to give damage to Spitter.

  • Select PushableBox in the Hierarchy
  • In the Inspector click the Add Component button (located at the very bottom) then type Damager in the field and select Damager

Note: The Damager Component tells any GameObject that has a Damageable Component on it (like a Spitter or a Chomper) to give it damage. There is more in-depth information on this system in the Components Documentation.

You’ll notice a green box appears in the Scene view. This is a collider and it signifies where it’s going to give damage.

  • Click and drag on the green dots on the edge of the collider box so that it encompasses the PushableBox

Tip: If the green or red move tool arrows get in the way of your ability to drag the collider into place, select the Hand tool (Q for keyboard shortcut) and the move tool arrows will disappear. Now you can arrange the collider correctly.

Lastly, we need to make sure that damage is given to the right GameObjects. We separate objects into Layers in the Editor so that they can easily be found and separated:

  • In the Inspector, locate the Damager component "Damager (Script)". In the Hittable Layers dropdown select Enemy

  • Enter Play mode, and use "D" to move Ellen forward -- she'll push the box onto Spitter to squash him!
  • Save your Scene! In the menu select File > Save Scene or press Ctrl + S (Windows) or Cmd + S (Mac)

Now let’s add our second enemy:

  • As you've done before, find or build a place to add an enemy, and move Ellen into the scene
  • In the Project window go to Assets > 2DGameKit > Prefabs > Enemies
  • Click and drag Chomper into your Scene
  • Play around with the settings in the Inspector view under Enemy Behaviour (Script) to adjust his Speed and View settings to your liking. Don’t forget that while in Play mode, you can press F12 to make sure weapons are enabled, and attack with O or K
  • Give it a try and save your Scene
  • When you complete Lesson 4, check it off at the bottom of the page!

In the Scene above, a Chomper is selected. We have adjusted his View FOV and View Direction so that he won’t spot Ellen until he turns around:

For more information on the Enemy Behaviour Component check the Components Documentation.

Supporting live training video

Lesson 4: Adding and squishing enemies

Note: The instructions for Lesson 4 automatically begin at 1:18 and end at 7:24. Adding Chomper is from 00:00-1:17. These parts of the video are what is covered in the text material above. If you also want to learn how to add a Health pickup, you can watch and follow along with the video starting at 7:25, but that's just extra credit!

Did you finish Lesson 4?

Go to homepage Continue to Lesson 5