Tuesday, February 28, 2017

Day 16

Today I continued my tutorial.

I made a way that the game would keep spawning waves of asteroids that never end until you die.

I started off by opening up 'GameController' which is the script I used for the spawning of the first asteroid and I modified it a bit.

I had to use something called a 'for' loop to keep creating asteroids until the criteria is met.
I made a public integer variable called hazardCount.

I did "for (int i = 0; i < hazardCount; i++) { //Asteroid spawning code}

What this does is it loops the create asteroid code as long as 'i' is smaller than 'hazardCount'. And it adds 1 to hazardCount every time it loops.

Now what this did was just make one big wave of 10 at once, what I want is to spawn a wave of 10 gradually then wait a few seconds and then spawn another wave.

So I wrapped the for loop in another loop called a 'while' loop. I said while (true) {//code here}.
Since the while loop is true it will always loop no matter what. Then I made a new public float variable called spawnWait. Now what I had to do is use 'Coroutines' as a way of pausing and resuming the loop. So inside the Start() method I typed "StartCoroutine (SpawnWaves());". I then had to make the SpawnWaves() method an 'IEnumerator' by changing 'void SpawnWaves()' to 'IEnumerator SpawnWaves()'. Then at the bottom of the while loop I add a 'yield return new WaitForSeconds(spawnWait)' that waits until the value of spawnWait in seconds is finished, then does the loop again until i = hazardCount. I set spawnWait to 0.5 so it waits 0.5 seconds before generating a new asteroid.

Now what I wanted to do was make it wait a certain amount of seconds in between each wave before it repeats the for loop. So I made a new public float called waveWait, and then after the for loop I added another  'yield return new WaitForSeconds()' but this time it was the value of waveWait. And I also wanted to wait a few seconds at the start of the game before the waves start spawning to prepare the player for the wave. I just did the same thing: made a public float 'startWait', and made a yield return you before the while loop.

This is the finished code:


Now I'm done and my waves are spawning nice and smoothly :D


Tomorrow I will be continuing the tutorial.


No comments:

Post a Comment