Click here to Skip to main content
15,892,480 members
Articles / All Topics
Technical Blog

Day 84 of 100 Days of VR: Endless Flyer – Adding Power Up Spawn Points

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Feb 2019CPOL5 min read 1.3K  
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.

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.

  1. 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
  2. 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
  3. 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; // from 0 to 1

	private string containerString = "Container";
	private string spawnPointString = "Spawn Points"; // string to find our Spawn Points container
    private string powerupSpawnPointString = "Powerup Spawn Points"; // string to find our powerup spawn points container
	private int numberOfCoinsToGenerate = 5;
	private int coinDistanceGap = 20;

	void Start () {
        SpawnCoin();
        SpawnPowerUp();
	}

    private void SpawnCoin()
    {
        Transform spawnPoint = PickSpawnPoint(containerString, spawnPointString);
        // We then create a loop of X items that are Y units apart from each other
        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()
    {
        // We randomly generate a number and divide it by 100. If it is lower than the spawn rate chance we set,
        // then we create the powerup.
        bool generatePowerUp = Random.Range(0, 100) / 100f < PowerupSpawnRate;
        if (generatePowerUp)
        {
            Transform spawnPoint = PickSpawnPoint(containerString, powerupSpawnPointString);
            // TODO we need to create a powerup to create.
        }
    }

    private Transform PickSpawnPoint(string spawnPointContainerString, string spawnPointString) 
    {
        // We get container game object and then  the spawnPointContainer and get it's children which
        // are all spawn points to create a spawn point. The benefit of this is so that we don't have
        // to manually attach any game objects to the script, however we're more likely to have our code break
        // if we were to rename or restructure the spawn points
        Transform container = transform.Find(spawnPointContainerString);
        Transform spawnPointContainer = container.Find(spawnPointString);

        // Initially I first used GetComponentsInChildren, however it turns out that the function is
        // poorly named and for some reason that also includes the parent component, ie the spawnPointContainer. 
        Transform[] spawnPoints = new Transform[spawnPointContainer.childCount];

        for (int i = 0; i < spawnPointContainer.childCount; i++)
        {
            spawnPoints[i] = spawnPointContainer.GetChild(i);
        }

        // If we don't have any spawn points the rest of our code will crash, let's just leave a message
        // and quietly return
        if (spawnPoints.Length == 0)
        {
            Debug.Log("We have a path has no spawn points!");
        }

        // We randomly pick one of our spawn points to use
        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

The post Day 84 of 100 Days of VR: Endless Flyer – Adding Power Up Spawn Points 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 --