We now have talked about how to get started in music inside of Unity and added some nice background music without the need of code! However, there are some situations where we don’t want to play an audio file all the time and where we want to control when to play a specific sound. We can achieve this through code! Let’s get started!
Setting up the Player GameObject
As we instantiate the Laser from inside the Player script, it is a good idea to set up everything audio related for the laser inside this script as well.
As we can assign…
You might have noticed me using the [Header] Attribute in some of the previous articles (like this one).
In this short article, I’d like to show you what this attribute actually does and why you should use it. Let’s get started!
Let’s take the Player.cs script as an example. To make it easier, I will just use the variables with a [SerializeField] before them, so we just have those that we actually see in the Inspector. We currently have the following variables matching these filters:
In the Inspector, those look like this:
In order to further increase the immersion of the player into the game, we can still add some more things other than sound effects and music. A camera shake when taking damage for example.
There is a possibility of achieving that with using only Cinemachine but that’s not what I do here this time because the lack of knowledge of that topic. Therefore, let’s just create a small script to achieve a camera shake behavior!
Let’s take a look at what we have to do:
Here’s how the script will look like in the end:
We all know this kind of situation: Your mid-game but all of a sudden our phone rings and you have to answer the call. Or, what might be more common, you have to go to the toilet. And if I have to take a guess here, you will pause the game in such a situation - if you’re not playing an online game I mean. Let’s implement such a feature in the Space Shooter as well to get you covered in such situations!
To manipulate how fast things are happening during gameplay, Unity has a method called Time.timeScale.
Okay, this challenge was quite challenging. Knowing nothing about how to implement such a behavior, there was only one way to take on this problem. Researching. Something called “weighted probabilities“ came up and that’s exactly what I needed. However, all the code snippets, explanations and documentations were a bit overwhelming for me. Therefore, I tried to look up some more detailed information about it and found this into-the-depths tutorial by GameDevHQ on YouTube. That’s when things started to click for me!
Let’s take on the challenge now.
As of now, the Space Shooter contains five powerups:
As we just implemented the ability to have multiple shields activated at the same time, it’s now time to add some details to it. We already have the UI elements which represent the current amount of shields. However, the shield visualizer on the player remains static. Let’s change that!
As a reference, that’s what we’re going to implement:
The implementation is done pretty quick! We just need a new method which will handle the opacity of the shield visualizer. For that, let’s take a quick look at how to change the color inside a script.
We need to access the…
We already have a mechanic to defend one hit from the enemies. Let’s tweak that a bit by maximizing the shields you can have to three.
I. Setting up the Player.cs script
I.1 - Variables
As first thing, we need to add a variable to store the current amount of shields we have. Let’s just call it _shieldCounter.
In this post, we will go over some improvements and tweaks for the Space Shooter game.
I. Adding a “Ammo+” PowerUp
Just recently, we limited the amount of available ammo for the player. However, there is no ability to refill the ammo. Therefore, the game would be limited to 15 shots and would be a plain survive-and-dodge afterwards… Until now. The player now has the ability to refill the ammo with the help of a new powerup.
As there was no pre-built ready-to-use powerup sprite for this one, I just created one by using the provided assets and playing around…
What’s a game without a challenge? I can’t name a game here where there was no challenge. Even casual games have challenges. Don’t get me wrong, challenges are something great in games. Let’s add a ammo restriction for the player!
Setting up the Player.cs script
As always, we need variables as basis for the logic. We need one for the maximum allowed ammo and one for displaying the current ammo the player has available.
II. Adapting existing methods
At the very beginning of the game, we should make sure to give the Player the maximum amount of allowed ammo…
The Space Shooter has a variety of sound effects and music now. To have a cleaner way of all sound effects in use, AudioManagers come in pretty handy! They avoid you getting confused about which sound is played through code and make it easy to keep track about how many sound effects we have in use.
Advantages of multiple AudioManager GameObjects
Using more than just one AudioManager has some advantages for you and others working on the project:
The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)