Adding Multiple Enemies

At the moment, we only have one soldier in the scene. In this lesson, you'll learn how to add more. To do that, we're going to have to solve a problem with our EnemyLookAt script.

To see what the problem is, click on your Soldier in the Hierarchy. Duplicate the Soldier by either right-clicking the soldier and selecting Duplicate from the menu, or simply press CTRL + D on your keyboard.

With the new soldier selected in the Hierarchy, move it to a new position.

What you'll find when you play the game is that, when you kill the first guy, the second guy will no longer follow your movements.

We're going to be deleting the EnemyLookAt script and moving its code to EnemyHealth. So it's easier to delete the soldier you've just added. Select your second guy in the Hierarchy and press the delete key on your keyboard to get rid of him.

Now go back to your EnemyHealth script. In the Update statement, add the line in bold:

private void Update()
{

if (currentHealth > 0 && isEnemyDead == false) {

transform.LookAt(player.transform);
ShootAtPlayer();

}

}

So we're looking at the player and then shooting at the player. In between the round brackets of LookAt, we now have this, though:
player.transform

So it's not just the player but the transform of the player, all his positional and rotational data, in other words.

One more thing to change in the EnemyHealth script. Locate this variable declaration at the top of your code:

public static bool isEnemyDead = false;

Change it to this:

private bool isEnemyDead = false;

We no longer need to have the isEnemyDead Boolean as public and static. It can be a private bool instead.

Save your code and go back to Unity. Select your Soldier item in the Hierarchy. In the Inspector on the right, you can remove the LookAt script quite easily. Click the three dots in the upper right of the LookAt script Component:

Removing a component from a game object

From the menu, select Remove Component:

The remove component menu in Unity

You can remove any component this way.

Now, in the Project area at the bottom of Unity. Click inside of your Scripts folder. Delete the EnemyLookAt script because you don't need it anymore.

In the Hierarchy, duplicate your Soldier again. Reposition him in the Scene. You might have something like this:

Two enemy soldiers in Scen view

Our new Soldier was placed at these positions:

Position

X: -5
Y: 0
Z: 7.5

Now play your game to highlight a problem. Dispatch the first soldier. Now walk along the corridor, hugging the right-hand wall. You may find that the soldier in the inner room can shoot you through the walls, as in the video below:

 

The problem is caused by the position of the Raycast coming from the soldier. To see where the ray is coming from there is a handy inbuilt method of the debugger called DrawRay.

Go back to your EnemyHealth code. After the if statement in the Update method, add this line:

Debug.DrawRay(transform.position, transform.forward * range, Color.red, 0.5f);

Your Update method should look like this:

Unity C# code for the DrawRay method

In between the round brackets of DrawRay, we have four arguments separated by commas:

transform.position, transform.forward * range, Color.red, 0.5f

The first argument is for the start position of the ray, the second argument is the direction. We want the ray to go forward. But we multiply it by our range variable, otherwise it would be too long. The third argument is for the color of the ray while the fourth argument is the duration - how long to take to draw the ray.

Save your code and go back to Unity. To see Draw in Action, drag your Game tab onto the Scene:

Arranging the tabs in Unity

You'll then have a split screen like this:

Two Unity tabs stacked on top of each other

(You can drag your Game tab back to the top anytime you like.)

Now play your game. You should see something like this, in the short video below.

 

In the video above, the ray is coming from the soldier's feet. This is a common problem. It means, if you have the slightest of gaps in a game object, the ray will go through that gap and hit the player. You could just lower the Y position of the walls so that the ray can't get through. But there is a better solution to be had and that's to raise the start point of the ray with code. That way, the ray won't be coming from the soldier's feet.

Go back to your EnemyHealth script. Add this float variable to the top of your code with all the others:

private float offset = 1.5f;

Locate the ShootAtPlayer method. Change this line:

Ray rayFrom = new Ray(transform.position, transform.forward);

To this:

Ray rayFrom = new Ray(transform.position + new Vector3(0, offset, 0), transform.forward);

And change your debug line to this:

Debug.DrawRay(transform.position + new Vector3(0, offset, 0), transform.forward * range, Color.red, 0.5f);

The part we've amended is for the first argument. It was this:

transform.position

Now it's this:

transform.position + new Vector3(0, offset, 0)

We're saying we want to change the Y position of the ray by the value in the offset variable, but keep the X and Z where they are.

You can't just do this:

transform.position + 1.5f

That's because the Ray needs a Vector3. So you add a new Vector3. In between the round brackets of Vector3, we have three values.

We only want to change the Y position, so keep the other two (X and Y) on zero. You can change the 1.5f in offset to anything you like. Higher numbers will raise the ray.

Save your code and go back to Unity. Play your game and you should find that the Ray is now higher up:

Rays drawn from two soldiers in Scene view

The ray now no longer sneaks through the bottom taking points off the player.

But DrawRay is a good method to know about, if you need to check where your rays are coming from and going to. (You can comment out your DrawRay line, though, or delete it altogether.)

Try placing a few enemies in your scene, see what happens. One thing you will notice is that are all static, with none of them running around. To get your players to run around, you need something called AI. Unity has an Artificial Intelligence system built into it. This is a more intermediate than beginner, so we have a separate tutorial on it. You can skip it, if you want. But it's here:

Unity AI

We'll move on, though, and tackle enemy accuracy. This will mean that the soldier doesn't wing you with evey shot. Plus you get to add a slider to the Inspector.

<--Back to the Unity 3D Course Contents Page