Interactivity : Workshop 7

/
0 Comments
To begin this lesson we added audio, sound effects and music to the game. 

With the asteroid explosion selected I dragged the explosion_asteroid sound effect from the Audio folder in Assets over to the game object in the Inspector. This created an audio source.

Audio folder with sound files

Audio source created in Inspector


We made sure that Play On Awake was ticked on so that the explosions would work properly, as this allows the audio to play automatically when the explosion in spawned. 


This process was also done for the explosion of the player, however as Play On Awake had been ticked beforehand, no further edits needed to be made. 

For the player's weapon sound effect, I dragged the audio from the folder on to the player game object in the Hierarchy. This added another audio source in the Inspector to the game object. However this time Play On Awake had to be de-selected to prevent the sound being played during the very first frame of the game.




Since the audio wouldn't be playing automatically, we had to edit the player controller script. audio.Play was added in order to achieve this.

Added coding

Full script with added coding


To add music to the game, I dragged the 'Music Background' clip from the Audio folder in Assets on to the Game Controller in the Hierarchy. In the audio source created, I made sure that Play On Awake was ticked on, so that the music would play on the first frame of the game, and also selected Loop so that the music would be continuous. 

Initial audio source added in Inspector

Play On Awake and Loop selected 

The final step with audio was balancing the sound. To do this I went back to the audio sources of the player's weapon and the background music and reduced the volume from 1 to 0.5. The explosions were kept at 1 to be significantly louder. 


The next thing we wanted to do was add text to the game, such as 'Game Over' and the 'Score'. To begin this I created a new empty gameobject. Then in the Inspector I added the GUIText from Add Component. I renamed this 'ScoreText' as this will be for keeping track of the score. In the text box in the GUIText component I also changed this to 'Score Text' in order to keep track of the positioning. This was originally centralised on the game, however changing the Pixel Offset I re-placed this in the top left corner. 

Adding score text

Initial Positioning


Final positioning

In the Game Controller script I added the following bits of coding :






In the DestroyByContact script I also added some more coding : 






In the Destroy By Contact script I changed the score value to 10, which means every time an asteroid is hit with a bolt, 10 points will be added to the score. 




Then with Game Controller selected in Hierarchy, I dragged the Score Text object from the Hierarchy onto the Game Controller script on to the tab 'Score Text.'


The result was that in game mode the score in the top left corner would increase. 

Score of 70 and going up!

A similar process was used to create Restart and Game Over text. 


This is the completed Game Controller script to make this work :



Similarly to adding the Score Text control, I dragged the appropriate texts from the Hierarchy and into their appropriate place in Game Controller script in the Inspector.


A few more coding was added to the Destroy By Contact Script, as done before for the score. This is the final script :



The result is that as soon as the player's ship is blown up by an oncoming asteroid, the Game Over text appears in the middle of the screen. Once the final wave of asteroids has passed and the screen returns to being empty, the text for restart appears in the top right corner, stating to press R to do so. 


Reflections

This is roughly the game finished. It has been so cool seeing it all come together and I've really enjoyed everything so far! Can't say I'm a master at coding yet, but I'm trying! Looking forward to editing it into my own way and seeing what else I can come up with before exporting it for assessment. 


You may also like

No comments: