Welcome to day 84, today in our Endless Flyer game we’re going to start implementing new features, specifically, we’re going to start implementing a power-up system.
This is going to be a multi-step process, but the first thing we’re going to look at today is the ability to spawn items randomly in the game.
After that, in the next couple of days, we’re going to look at implementing different types of power-ups that you might see in an infinite runner type game. Things like magnets, invincibility, etc.
Step 1: Adding Power Ups To The Maze
Similar to how we added coins into our map, we’re going to do something similar to generate power-ups in our game.
Currently, we don’t have any other options, so today we’ll just create the scripts and add the variable in. You don’t have to follow along what I’m doing, I exported a package that has the game object which you can find in the CubeFlyer repo, it’s called ComplexPathsWithPowerUps.unitypackage.
But let’s get started:
Step 1.1: Adding the Powerup locations to the map
Just like we did last time when we were adding coins, we’re going to go through all the map prefabs and add a spawn point for where we want to create our powerups.
- Grab one of the path prefabs that we have and go to Complex Pillars > Container and create a new object and call it Powerup Spawn Points
- Create another game object that’s the child of the Powerup Spawn Points and call it Powerup Spawn Point 1 and then move it somewhere in the path
- Back to the path, in this case, Complex Pillars 1, and then in the Inspector, hit Apply to save our changes to the prefab.
Now do this for our other 11 spawn points.
For now, these spawn points won’t do anything, but we’re going to create a script that will put a random powerup in those locations.
Once again, I’m not going to document the whole process, just put them yourself or take the ones I’ve made in the CubeFlyer repo.
Here’s an example of what it looks like in the hierarchy:
Step 1.2: Spawning the Power Up Item with the PathItemGenerator
Now that all of our paths have a Power Up Spawn Point, we need to use it.
We could make a new script, but luckily for us, we already have an existing script that’s used in all of the Path game objects to generate our coins.
We’re going to make use of that to also spawn our Powerups!
Specifically, the script is PathItemGenerator.cs it is attached directly to the parent most object in the Path game objects that we have.
We’re going to need to do some refactoring. A lot of the code that we currently use to generate coins can also be used to create power-ups!
Here’s what it looks like now:
using UnityEngine;
public class PathItemGenerator : MonoBehaviour {
public float PowerupSpawnRate = 0.2f;
private string containerString = "Container";
private string spawnPointString = "Spawn Points";
private string powerupSpawnPointString = "Powerup Spawn Points";
private int numberOfCoinsToGenerate = 5;
private int coinDistanceGap = 20;
void Start () {
SpawnCoin();
SpawnPowerUp();
}
private void SpawnCoin()
{
Transform spawnPoint = PickSpawnPoint(containerString, spawnPointString);
for (int i = 0; i < numberOfCoinsToGenerate; i++)
{
Vector3 newPosition = spawnPoint.transform.position;
newPosition.z += i * coinDistanceGap;
Instantiate(ItemLoaderManager.Instance.Coin, newPosition, Quaternion.identity);
}
}
private void SpawnPowerUp()
{
bool generatePowerUp = Random.Range(0, 100) / 100f < PowerupSpawnRate;
if (generatePowerUp)
{
Transform spawnPoint = PickSpawnPoint(containerString, powerupSpawnPointString);
}
}
private Transform PickSpawnPoint(string spawnPointContainerString, string spawnPointString)
{
Transform container = transform.Find(spawnPointContainerString);
Transform spawnPointContainer = container.Find(spawnPointString);
Transform[] spawnPoints = new Transform[spawnPointContainer.childCount];
for (int i = 0; i < spawnPointContainer.childCount; i++)
{
spawnPoints[i] = spawnPointContainer.GetChild(i);
}
if (spawnPoints.Length == 0)
{
Debug.Log("We have a path has no spawn points!");
}
int index = Random.Range(0, spawnPoints.Length);
return spawnPoints[index];
}
}
Looking at the fields
In this script, we added 2 new fields that we’re going to use.
- public float PowerupSpawnRate – this is a value from 0 to 1 that we can change that will decide the chances of a power-up Since we don’t want a power up to be appearing every map, I decided if it appears 20% of the time will suffice. This means that it’ll appear 0.2 of the time.
- private string powerupSpawnPointString – this is the string of the container that we use to store the power-up spawn points
Walking through the code
We keep a lot of the code that we already had, we just had to move them to a separate function so that we can re-use part of the code that we have written.
- We begin in Start(), before, we just generated a coin, instead, we created 2 functions SpawnCoin() and SpawnPowerUp().
- SpawnCoin() is the already existing code we already know. The biggest difference is that I moved the code we use to get a spawn point to PickSpawnPoint() where we give it the string to use to find the correct spawn points (coin vs power up) that we want to use. And then once we get the results, we’ll use our selection to generate our coins like what we did before.
- In SpawnPowerUp() we call PickSpawnPoint() with the Power Up Spawn Point string that we set up. The first thing we do is we pick a number from 0 to 100 and then divide it by 100 and then compare it with our spawn rate. If it’s below our spawn rate then we should create our Powerup. We do that by calling PickSpwanPoint(). Once we get the spawn point we want to use we’ll create our power up. Unfortunately, at this point, we don’t have any power up to create so I left a TODO note.
- Finally, in PickSpawnPoint() we use our existing code that gets our spawn point container and then randomly picks one of the game objects that we had in there to be our chosen spawn point.
End Day 84
And that’s it! Today’s another relatively short day, however, we’ve gone and set up the foundations of creating power-ups in our game.
We:
- Added the power-up spawn points in our paths
- We changed out item generator script to also generate power-ups
We don’t currently have any items that we can generate, however in the next couple of days we’re going to start creating power-ups for us to randomly generate that will allow us to have a more interesting game.
The first one we’re going to look at is a magnet power up! I’ll see you all in the next post!
Day 83 | 100 Days of VR | Day 85
Home
CodeProject
The post Day 84 of 100 Days of VR: Endless Flyer – Adding Power Up Spawn Points appeared first on Coding Chronicles.