Interactivity - Lesson No.5

Within this lesson we learnt how to make the spaceship and the asteroids explode when they either come in contact with each other, or if the asteroids came in contact with a bolt. We also looked into making the asteroids move and spawn in waves towards the player, which is the first time it started to look like an actual game.

Making the Asteroids disappear when shot

The first issue we resolved was making the asteroids disappear when they are hit by the bolts, because both of the objects have trigger colliders a new script has to be created so the effect can happen, so as per usual a new script has to be created and renamed to DestroyByContact.

That was the code that is in the script, its basically saying that when one collider hits the other they need to disappear (destroy). However within the script there is the if statement beginning "if(other.tag == "Boundary")", this is because the Boundary gameObject is causing the asteroid to disappear. To get over this the Boundary gameObject needs to be tagged as Boundary and this code needs to be entered in as it will tell Unity to ignore the Boundary and focus on the asteroid. 

Explosions

We then went onto adding the explosions prefabs onto the asteroids and the players spaceship, first of all we added it onto the asteroid by editing the DestroyByContact script to the following:

We can then link in the asteroids explosion so that once it has been hit by a shot it will explode, this is done in the inspector. Basically click on the little circle next to the explosion slot within the inspector screen and find the asteroid explosion prefab, then when you play the game the asteroid should explode. 


The next part is to make the players spaceship explode when it hits the asteroid, there are two parts to this because when the player explosion is first added onto the it also happens on the asteroids as well and that is not what is meant to happen. This part is also edited into the DestroyByContact script and should look like this at the end result:

Basically the Instantiate method was copied from the explosion part of the script and changed to playerExplosion, also a new public GameObject was entered in. The if statement is there to stop the explosions going off at the same time on the asteroids, and it is under the explosion code block because it should be looked at afterwards by Unity. 
Then, same as before, the player explosion VFX needs to be added in, so on the inspector panel click on the circle next to the player explosion slot and find the correct VFX prefab.




Moving the Asteroid

This was actually the most basic part of making the video game so far, all that is needed is to attach the Mover script created before for the shots to the asteroid. Then the speed needs to be set to -5 so that the asteroids move towards the player, once this is done drag the asteroid down into the prefab folder to make a prefab and delete the one in the Hierarchy.


Creating Waves of Asteroids

Now that the asteroid prefab is made it is time to create waves of asteroids to create a challenge for the player, to do this a new gameObject needs to be made, renamed to Game Controller, reset in position, and tagged as GameController. 
Attached to this needs to be a new script named GameController, the primary task of this script is to spawn asteroids. The script so far should look like this:


Then attach the asteroids prefab to the script in the same ways as the explosions, within the inspector panel and by clicking on the little circle next to the hazards place. Next the spawn values need to be changed within the inspector panel, the z value needs to be off the top of the screen and the x value needs to be off the side of the screen. This is also changed in the script as the y value needs to be different due to the asteroids needing to be spawned at random y values. 



Also added into the script is something to allow the asteroids to spawn at different times so that they don't all explode at the top of the scene, plus extra code to allow the waves to spawn with a short wait between them. All these values need to be changed within the inspector panel, the recommended values are:
  • Hazard Count: 20
  • Spawn Wait: 1
  • Start Wait: 5
  • Wave Wait: 5

The game itself should now play like this:




Unknown

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment