Full Unity 2D Game Tutorial 2019 – Interfaces and Enemy Shooting

Sharing is caring!

Full Unity 2D Game Tutorial 2019 – Interfaces and Enemy Shooting

In this section of the tutorial we will implement the enemy shooting capability and we will use Interfaces to allow us to add the ability to give and take away health to an object. We will also look into changing the collision matrix to allow us to limit which colliders should interact with each other.

Full Unity 2D Game Tutorial 2019 – Enemy Shooting

First let’s allow our enemy to shoot by using the code we already created for the player.

Code Notes:
Nothing really new here, just using the same code as the player with some small adjustments.

We have copied the code from our player here and changed it so we use the target position instead of the mouse click position to direct the bullet. Since we have duplicated this code in the player and enemy we should really extract it from the player and enemy and put it in its own class which the player and enemy can share but for now lets just leave it and add a TODO.

You can test the game out now and remember to set the weapon for the enemy in the Unity editor.

Yay! the enemy shoots when the player gets close enough, but nothing happens when the bullets hit each other. One of the reasons for this is that the bullets don’t even have colliders on them. They are just images floating across the screen. To make them physical object we need to update our bullet prefab to have a collider. Make sure to set it to be a trigger as we will use the onTriggerEnter method to trigger getting hit.

Bullet Collider - InterfaceThat’s one issue sorted. Another issue is we don’t have any code in place to take care of taking damage. We will create an Interface which we can place on any object we want to take damage.

Full Unity 2D Game Tutorial 2019 – Interfaces

An interface is a bit of code that says what methods should be added to a class in order to interface with other classes. Our Interface will be called IEntityHealth and will say what methods an object needs to have to be able to change its health. The code is pretty simple but the concept is a bit harder to grasp. We first start by making the Interface. In the project window add a new C# script and name it IEntityHealth (we tend to have all interfaces start with a capital I so we can identify them easily). Now add this simple code:

Code Notes:
We removed the class and monobehaviour because we will not be a game object or a class, we are an Interface.
Next we define the 2 methods we want our classes to have and what values they will take.

We have created the Interface but we don’t have a class that uses an interface yet. We will be adding them to both our player and enemy.

Enemy Code:

Player Code:

Now that our player and enemy have the new interface methods we just need to call them when the bullets hit them. Again we will implement some code for this, this time inside the bullets script. Simply add this method inside the Bullet script.

Now if you test out the game it looks like nothing happens. and our enemy and player die for no reason. This is because the player bullets hit the player and the enemy bullets hit the enemy. They are killing themselves 🙁

Full Unity 2D Game Tutorial 2019 – Collision Matrix

We don’t want our player and enemy to kill themselves. We often don’t want certain things to interact with one another, like the player and the players bullets or one enemy’s bullets hitting another enemy. This is where the collision matrix comes in to play. To use a collision matrix we must create some layers where we can separate objects. Click an object in the hierarchy view and click the layer drop down and finally click Add Layer… to start adding some layers.

Layers

Add 2 new layers by typing text in the text boxes presented to you. We will have one for player and on for the enemies. Here my layer 8 is the player, we will need to remember this later as we will use this value to set what layer our bullets will be added to.

Layers order

Now we need to update our player and enemy scripts so they set the layer for the bullets. All we have to do is after we create our bullet set its layer to the layer we want it on. This is the code for the player:

Simple right, now do the same for the enemy. We should also change the layer of our player and enemy to their respective layers. And if requested change the layer of all children.

Player layersNow if we run our game.. nothing has changed. That’s because we haven’t changed the matrix yet. We need to update it so it knows which layers will interact with each other. To do that we start by opening the project settings window. We can open it by going to the Edit > Project settings then selecting Physics 2D and the matrix is displayed at the bottom.

Project settings

Update the matrix so players don’t interact with other player objects and enemies don’t interact with other enemies like the image below.

Matrix AdjustmentNow if we run our game we will see that the player and enemy can now shoot without hitting themselves. We still have an issue though, our player bullets are hitting our enemy from far away. This is cause by the trigger we have used in the enemy to spot the player. Its our LineOfSight trigger and this can be easily fixed by moving the trigger from being directly on the enemyObject to a child.

All we need to do is create an empty inside the EnemyObject and name it LineOfSight. Then add a CircleCollider2D and set it to a radius of 5. Then delete the CircleCollider2D component on our enemy that represents the Line of sight.

Enemy Line Of SightWith all this done you should end up with something similar to this

Final Product

As usual the complete source can be found on Github here.

Previous Part  –   Next Part

Sharing is caring!

2 Replies to “Full Unity 2D Game Tutorial 2019 – Interfaces and Enemy Shooting”

  1. Bullets don’t destroy themselves. Nothing takes damage. The empty object still triggers the collider. I give up.

    1. Maybe you forgot to set the “isTrigger” checkbox on the bulletPrefab’s collider? That was what did it for me!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.