Wednesday, March 1, 2017

Day 17

Today I continued the tutorial.

I added sound effects and music and I added a score text at the top of the screen that displays your total score. Everytime you destroy an asteroid it adds an amount to your total score.

I grabbed the sounds from the asset pack and dragged the asteroid explosion sound into the actual asteroid explosion prefab. I then set the check to play on awake.


What this does is it plays the sound on the first frame as soon as the prefab is spawned (so whenever the asteroid is destroyed). I did the same thing for the player explosion: I added the sound file to the animation prefab.

But for the weapon fire sound effect it's not as easy. I attached the sound to the player object but I set play on awake off. I had to actually open up the player script and do the code audio.Play() in the if statement for left clicking to shoot. What this does is everytime I shoot it plays the sound that is attached to the object.

Unfortunately I can't let you hear the sounds as the blogger doesn't support sounds. But trust me the sounds are there :P

After I made the sounds, I started working on the score.

I made a new GUIText and named it Score Text. Then I opened up the game controller script and worked on the code. I made a public GUIText variable and a private score integer variable. Then I made a new private method called UpdateScore() and inside I wrote "scoreText.text = "Score: " + score;". After, I made a new public method called AddScore(int newScoreValue) and inside I made newScoreValue += score so that score would be updated, then I called UpdateScore() inside of the method.

But I'm not done yet. I have the go inside of the DestroyByContact script and add score everytime you destroy the asteroid. I made a new public integer variable called scoreValue and inside the OnTriggerEnter method I typed gameController.AddScore(scoreValue);. Now back in Unity I set the scoreValue to 10 so that it adds a score of ten everytime I destroy an asteroid. I then moved the GUIText to the top left of the screen and dragged it on the GameController script for the public GUIText.

Now I'm finally done :)

Tomorrow I will continue the tutorial.

No comments:

Post a Comment