Click here to Skip to main content
15,860,972 members
Articles / Game Development / Unity

Day 27 of 100 Days of VR: Adding New Bandit Enemy

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
16 Oct 2017CPOL8 min read 4.7K   2  
Adding new bandit enemy

Welcome to Day 27!

Right now, we have a relatively complete game and in my mental roadmap, we only have a couple more things to do before we start exploring how to use VR and then implementing that back into this game.

There will be a lot of fun times ahead!

Our goal is to create a new enemy. Specifically, for practice, I want to create:

  1. A lower health, but faster unit
  2. A higher health, but slower unit

With these new enemies, we’ll have a more diverse and interesting experience in our game!

Today, we’re going to focus on creating our speedy unit.

Step 1: Acquire New Assets

The first thing we need to do is go to our favorite place in the world! The Unity Asset Store!

We’re going to look at asset to be used as our enemy. When looking for new assets, the most important thing for us is to find something that’s mecanim ready.

If you’re not aware of the glory that is the mecanim system, you can visit Day 11 where we first used it.

The basic summary is that all Unity models can have rigged body structure that can share animations from other models with similar body structures without doing any extra work on our end.

Step 1.1: Get Bandit Asset

Browsing around the asset store for an enemy to use, I found a nice bandit to use for the speedy enemy!

It’s called Basic Bandit:

Image 1

Download and import the bandit into our game. It’ll be in Assets > BasicBandit.

Step 2: Setup BasicBandit

Now that we have our assets, the next thing we need to do is to setup up our BasicBandit asset to use our existing animation and scripts!

This work will be the same as what we have done with the knight model, except we don’t have to code anything this time!

Step 2.0: Set Wave 1 SpawnManager to Spawn -1 Enemies

Not necessary, but for the ease of testing our new enemy. If we set Wave 1 of our SpawnManager, to spawn -1 enemies, we won’t create any new enemies and the wave won’t progress, allowing us to experiment around with our enemies.

Another option is to comment out the instantiation code in SpawnManager code to not create any new enemies.

Image 2

Step 2.1: Attach the Knight Animator Controller

The great thing about Unity’s mecanim system is that we can re-use existing animator controllers for models that share the same animation.

In this case, because our knight and bandit are both humanoids, we can share our Knight Animator Controller with our bandit.

Now let’s do it!

  1. In Assets > BasicBandit, drag and drop Basic_BanditPrefab into our game.
  2. Select our bandit in the hierarchy and in the Animator component, under Controller, select our Knight Animator Controller to fill in the slot.

You’ll have something like this now:

Image 3

Now with this, our bandit will have the same animation rules as our knight (running, attacking, death, and idle).

Step 2.2: Add the Nav Mesh Agent and the 3 Scripts: EnemyHealth, EnemyAttack, and EnemyMovement

At this point, we want to re-create everything that’s attached to our Knight and move it to our bandit.

We want to add the following things to our BasicBandit by clicking Add Component in the inspector:

  • Nav Mesh Agent component to be the navigation AI to make the bandit chase after the player
  • EnemyHealth: to receive the damage when the player shoots the enemy
  • EnemyAttack: to detect when the enemy injures the player
  • EnemyMovement: to control the movement logic for the enemy

Step 2.3: Setting Up the Scripts and Nav Mesh Agent

Now that we have attached our script, we need to set them up.

Nav Mesh Agent

For our Nav Mesh Agent component, we want to adjust the speed of how fast it moves.

Let’s set the Speed to be 3.25. Our Knight’s speed is 3.

Image 4

EnemyHealth

For our Enemy Health, we want to set:

  • Health: 5
  • Hit Sfx Clips > Size: 4
  • Set the 4 new Audio Clips to be Male_Hurt_01Male_Hurt_04
  • Hit Sound Delta: 5

Image 5

EnemyMovement

For the EnemyMovement scripts, we want to set:

  • Walking Clips > Size: 4
  • Set the 4 new Audio Clips to be Footstep01 – Footstep04
  • Walking Delay:3

Image 6

EnemyAttack

We don’t have everything for our EnemyAttack script, but let’s set what we can first.

  • Attack Sfx Clips > Size: 3
  • Set the 3 new Audio Clips to be Punch_01Punch_03
  • Leave the Fist Colliders alone for now

Image 7

Now if we were to play the game, our Basic_Bandit will start chasing after us, but he won’t attack us yet, we haven’t set up our triggers and colliders yet!

Step 2.4: Adding 3 Colliders: Trigger Collider, Mesh Collider, and Fist Colliders

The final thing that we need to do is to add colliders to our bandit that will help us detect when the player is close, when the enemy damages our player, and when they get hit.

To do a re-cap the purpose of what we need to do, we need:

  • A Capsule Collider to be used as a trigger to tell our bandit to enter attack mode if he’s near the player.
  • 2 Box Collider that will be attached to the bandit’s hand to tell when we damage the player.
  • A Mesh Collider to be used for detecting when the player shoots the bandit.

Let’s add them in.

Adding the Capsule Collider

Select Basic_Bandit in the hierarchy and then add the Capsule Collider component.

We’ll make it a trigger, set the Y to be 1, and then expand Radius to be 1.5.

When you’re done, we should have something like this:

Image 8

Note: The radius is the outer sphere that you see.

To do a re-cap, whenever our player enters the circle, in our EnemyAttack script, we’ll hit OnTriggerEnter() which is used to tell the bandit that it’s in range to be able to hit the player.

Adding the 2 Box Colliders

Next up, we need to add 2 Box Colliders to our Bandit’s fists.

Select Basic_Bandit > Armature > Hips > Spine > Chest > Shoulder.L > UpperArm > LowerArm > Hand.L

We want to add a Box Collider to Hand.L.

In our Transform, we’ll keep position the same, however, let’s change the Size of our Box Collider:

Size (X, Y, Z): 0.1, 0.3, 0.1

Finally, just attach the Fist Collider Script to Hand.L.

When we’re done, we’ll have something like this:

Image 9

Now do the exact same thing to the other hand in Hand.R.

Adding the Mesh Collider

Next up is the Mesh Collider component.

As we learned in Day 20, adding our collider into the parent object will not work, we should put the collider in the location of the mesh itself.

This would serve us 2 purposes:

  1. We can shoot the enemy
  2. The enemy can push us around

In the bandit’s case, we have 2 pieces: the body and the clothes.

Image 10

Image 11

What you see above is the Mesh Collider in each of the pieces of the mesh of the bandit.

Let’s get started!

In BasicBandit_Body, add a Mesh Collider component, under Mesh add the mesh BasicBandit_Clothes. With this, you’ll have the 1st image above.

Here’s what it should look like:

Image 12

Next:

In BasicBandit_Clothes, add a Mesh Collider component, under Mesh add the mesh BasicBandit_Clothes. With this, you’ll have the 2nd image above.

And here’s what it should look like:

Image 13

At this point, we’re almost there.

We have the colliders to detect when our bandits get hit, however, if we were to try and shoot our enemy, we wouldn’t “hit” him yet.

Step 2.5: Set Bandit’s Layer to be Shootable

As you might recall when we were shooting enemies with Raycasting in PlayerShootingController, we did an optimization to only hit objects that have a layer mask set to Shootable.

Select our bandit in the hierarchy and set our Layer to be Shootable.

Image 14

Now with these colliders set, we can shoot the bandit and they’ll take damage and be knocked back.

Step 2.6: Attach our Fist Colliders to EnemyAttack

Earlier on, we attached our First Collider script to the bandit’s hands. It’s time to attach both of our hands to our EnemyAttack Script so we can detect if either of our the bandit’s fists makes contact with the player in Attack().

In EnemyAttack set:

  • Left Fist: L
  • Right Fist: R

Step 2.7: Create a prefab of our Bandit

Now that we have everything setup, we’re going to make a prefab of our finished bandit model.

Drag our finished copy of Basic_Bandit in our hierarchy into Assets > Prefabs.

Let’s also rename our prefab to be just called Bandit.

Image 15

Conclusion

Now with all these changes added in, we will have a fully functional Bandit enemy!

I do want to mention that the game won’t function perfectly, for example, if we were to get killed by the Bandit, he’ll continue to attack us.

The reason is that our Bandit isn’t in the Enemy Container game object, which means he won’t be disabled.

In fact, our current Bandit isn’t parented by anything at all! That’s why he doesn’t stop after the player character dies.

We can fix that by removing the bandit and adding him as one of the spawns for our SpawnManager, however before we do that, I want to create one more enemy to use.

Tomorrow, we’re going to create our last and final enemy. Until then!

Day 26 | 100 Days of VR | Day 28

Home

The post Day 27 of 100 Days of VR: Adding New Bandit Enemy appeared first on Coding Chronicles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Joshua is a passionate software developer working in the Seattle area. He also has experience with developing web and mobile applications, having spent years working with them.

Joshua now finds his spare coding time spent deep in the trenches of VR, working with the newest hardware and technologies. He posts about what he learns on his personal site, where he talks mostly about Unity Development, though he also talks about other programming topic that he finds interesting.

When not working with technology, Joshua also enjoys learning about real estate investment, doing physical activities like running, tennis, and kendo, and having a blast with his buddies playing video games.

Comments and Discussions

 
-- There are no messages in this forum --