Hi! I’ve been trying to add a “thud” sound effect to my rocket whenever it lands on the Launch Pad, but I can’t seem to get it to work. Below is my code and I tried to follow closely what has been done with the “success” and “crash” sound effects as shown in the video. Please help!
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionManager : MonoBehaviour
{
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip success;
[SerializeField] AudioClip crash;
[SerializeField] AudioClip thud;
AudioSource audio;
bool isTransitioning = false;
private void Start()
{
audio = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
if(isTransitioning)
{
return;
}
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("This object is friendly.");
ThudSound();
break;
case "Finish":
Debug.Log("Congratulations! You've finished this level!");
StartNextLevel();
break;
default:
Debug.Log("Wasted!");
StartCrashSequence();
break;
}
}
void ThudSound()
{
isTransitioning = false;
// To do: Add particle effect upon crashing
GetComponent<Movement>().enabled = true;
audio.PlayOneShot(thud);
}
void StartNextLevel()
{
isTransitioning = true;
audio.Stop();
// To do: Add particle effect upon crashing
GetComponent<Movement>().enabled = false;
Invoke("NextLevel", levelLoadDelay);
audio.PlayOneShot(success);
}
void StartCrashSequence()
{
isTransitioning = true;
audio.Stop();
// To do: Add particle effect upon crashing
GetComponent<Movement>().enabled = false;
Invoke("ReloadLevel", levelLoadDelay);
audio.PlayOneShot(crash);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void NextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
}