There is no game without sounds and particle effects, this is what gives life, along with other details, to our game.
How to add Sounds to our project:
First of all, Audio Source component has to be added to GameObject, then we create a folder where my sounds will be placed:
Afterwards, we declare [SerializeField]:
[SerializeField] private AudioClip engineThrustSfx;
[SerializeField] private AudioClip crashSfx;
[SerializeField] private AudioClip successSfx;
And we drag and drop our sounds to newly created [SerializeField] in Unity Inspector, which were declared in Movement.cs:
We write code where we do Caching, so we receive Audio Source reference:
private AudioSource _audioSource;
private void Start()
{
_audioSource = GetComponent<AudioSource>();
}
And we are good to go. From now on, we can easily implement our sound effects using logic. For example:
_audioSource.PlayOneShot(audioClip)
We call PlayOneShot (this method plays sound once) method on _audioSource and pass audioClip custom argument, which contains the sound. Or this example:
private void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Finish":
StartCoroutine(HandleAfterDelay(isNext: true, reload: false, audioClip: successSfx));
successParticle.Play();
break;
default:
StartCoroutine(HandleAfterDelay(isNext: false, reload: true, audioClip: crashSfx));
crashParticle.Play();
break;
}
}
Let’s break down case “Finish”. When a collision happens, and collision.gameObject.tag is “Finish” this means in my game Player landed on the landing pad, and it’s time to load next scene, which happens with a coroutine. We pass a bunch of arguments and pass sound (which is declared above). Then we handle it in IEnumerator function below:
private IEnumerator HandleSceneAfterDelay(bool isNext, bool reload, AudioClip audioClip)
{
// disabling Movement.cs forces to execute OnDisable method
_movementScript.enabled = false;
_rb.isKinematic = true;
if (audioClip)
_audioSource.PlayOneShot(audioClip);
yield return _delay;
Helpers.HandleScene(isNext, reload);
}
So if audioClip is not null, play the sound when the scene is handled.
This process is similar to adding Sounds. Here is a screenshot of my Player Object:
Rick’s Weird Space Rocket was provided to me from Udemy course I follow. So here we have 5 particles. Explosion executes when Player hits Obstacle, Success when we safely land on the landing pad, and the rest are thrusters. All of those particles have this Component named Particle System.
This is quite large system that allows you to create custom particles right in Unity.
Rest of the implementation is same as in Sounds. You declare variables, drag and drop them, and use them in your logic. They even share same methods like Play() or Stop().




Top comments (0)