Creating a balanced Spawn Manager in Unity.

So I’ve added a lot of things to this Space Shooter including 6 powerups and 1 powerdown. Now it’s time to get some balance. At the moment I have 2 of the items using their own script to give some weighted balance. The missiles and the powerdown, so I won’t include them in this new balance. I’m happy with how they are spawning.

You could do that actually, just set a whole script onto a powerup that will calculate a random number and use that to spawn. But that isn’t going to really balance anything and it’s also a little hungry on the cpu crunching all of those numbers. So I’ve decided to go in and create a new function that will keep this data crunching in one script.

Before I begin I highly suggest you go and watch this short video from GameDevHQ head, Jonathan Weinberger. It’s how to create a random table based on weights and I used this to create my balanced spawn manager.

I’m going to give you this code all in one screenshot and then go through what it does on the other side.

So I created this inside of my SpawnManager script. Basically I created this and then just called this when my _stopSpawning was false.

Yeah, I hear ya. I didn’t either until I started to work with it and it made sense pretty quick. I also read a colleagues medium on the matter that really clicked with me. Steve the dragon, a talented guy:

So to start I’ll explain what a weighted table does exactly. It’s set to go up to 100 (or 1000 or whatever, but try to make it easy math). In my case I chose 100 and then set about weighting my powerups accordingly. I thought Ammo should be the highest, then speed, health, shield and triple shot. So I created an array table and gave each that number and once calculated they should equal 100. As you can see I gave each a number out of 100 with triple shot only having an 8 which would be the lowest.

So you are probably wondering “if you have a random generator out of 100 what if it rolls a 65 what would that mean? So that’s where our math comes in.

Our highest rating is a 40. This code would ask is 70 lower than 40? No? Then subtract 40 from 65. That would give us 30. Then it checks that 30 against our next lower number, 25 and if that is lower. No? Then subtract 25 from 30 which gives us 5. Then it will check that 5 against our next number which is 16. Is 5 lower than 16? Yes! So it would award the health powerup!

Now, go back and read that paragraph again because I’m telling you it’s not as confusing the second time (and watch Jonathan’s video, he has a great way of explaining things).

So once we have all of the weights we combine them into the variable we created _weightedTotal. Once it spits out a random number it compares against each number on the list and runs the confusing paragraph above (it’s not confusing!!).

Once a powerup is awarded it will place into the poweruptoaward and will chose the one accordingly.

So that’s it, that’s that. It isn’t as hard as you might think. Once you have that core concept of the weighted table down it’s easy to figure out. Again, I implore you to watch the video — my medium article will give you a headstart but once you see it in action it will really click.

More power for our enemies! That’s right — i’ve decided to randomize the enemy and give some a shield that will take 1 laser to destroy. I’ll be showing you how to implement that tomorrow.

Happy Clicking!




With Unity all can be accomplished!

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

Recommended from Medium

Playing around with Routing Table

How to create a MYSQL database with WebFaction and PHPMyAdmin

PPL 2020 — Document Builder: React + Express + Docker: Is It a Recipe for Success?

Machine Learning Model Practice

Keen IO’s Cached Datasets: A Primer

How to Install a Transmission Mount

How dApps Are Shaping The Future Of SaaS And Software Development

E-book: Python® Notes for Professionals book

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
Laine Dorchester

Laine Dorchester

With Unity all can be accomplished!

More from Medium

Altering Texture Maps (Game Dev Day 73)

How We Implemented Vehicle Mechanics in Our Game — Part 5

Call vs Apply vs Bind

Learn to code by making a 2D space shooter game.