48: Balancing enemies

Photo by Sven Mieke on Unsplash

Objective: Add a weighted system for the Enemies

Now that we have a new enemy, let’s balance them so the basic enemy spawns a lot more than the ZigZag enemy.

The first thing we will do is add a property in the Enemy class to take a weight. We’ll make a public int _enemyWeight. I am leaving it public because later I’m thinking about implementing a system that’ll increase the weight of each enemy.

Next will be to move over to our spawn manager. We need to change our enemyPrefab to an array, so it’ll hold multiple enemies. Also, add a new variable for the enemy that got selected, as well has having a int variable to hold the total enemy weight.

Following this we’ll need a function to add all the weights of the enemies up. We’ll name this TotalEnemyWeightAmount. It uses a For loop to loop though all the enemies in the array and adds all their weights up and save to the total enemy weight

Next is a function that’ll select the enemy to spawn. In this function we need a random number generator, it’ll generate a number between 0 and totalEnemyWeight.

Than a foreach loop is used to go though each enemy again and see which enemy weight is greater than the random number. Which ever is greater than the random number will be saved into the selected enemy variable and passed on.

To finish it up, we need to call these functions. We’ll call the TotalEnemyWeightAmount in the StartSpawning function, so it’s not being recalculated every frame.

For the PickEnemyToSpawn will be called in the Coroutine that spawns our enemies.

In the Instantiate function we will swap out the enemyprefab that was there and add _selectedEnemy as shown above.

That’s a quick guide on how to create a little more balanced enemy system. Thank you for reading!




I am passonate on learning to program and use Unity to become a skillful Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Should We Use the App Bundle? Release App Bundle on AppGallery

Jetpack compose — Dependency injection with Dagger/HILT

Hands on with Jetpack’s Security App Authenticator library

Coin Hunt World v1.0.2.131

Lifecycle for Android Developers (Part 2)

Understanding the Constraint Layout in Android

Android Continuous Integration and Coverage visualization on Gitlab.

Creating the Twitter image Display layout with recycler view

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

More from Medium

Adding a burst laser powerup

Day 99: Character Animation Part 2

Unity : use Raycast for move the Player

Loading Scenes In Unity