Okay, I’ve tried everything I can think of, hopefully one of you will see what I’m missing.
My Main Engine and Death Sounds play as expected. When I land on the landing pad the game advances to the next level, but no sound plays. I’ve tried replacing the Success Sound with Thrust and Death, which work in their assigned spaces, but they won’t work for the Success event. This leads me to believe that it is the code, not the sound file itself.
I’ve made no modifications to the sound settings in Unity. I have checked the prefab and instances of my rocket. I have tried both my game scenes and my sandbox scene, same result everywhere. Apply thrust, sound. Hit obstacle, sound. Land on landing pad, no sound.
On the Prefab:
My Code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour {
Rigidbody rigidBody;
AudioSource audioSource;
enum State {
Alive,
Dying,
Transitioning
};
State state = State.Alive;
[SerializeField] float rcsThrust = 250f;
[SerializeField] float mainThrust = 1f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip deathSound;
[SerializeField] AudioClip successSound;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
RespondToThrustInput();
RespondToRotateInput();
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive)
{
return;
}
switch (collision.gameObject.tag)
{
case "Friendly":
//Do Nothing
break;
case "Deadly":
//Kill you
StartDeathSequence();
break;
case "Finish":
//Successful Landing
StartSuccessSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transitioning;
audioSource.Stop();
print("Play Ding!");
audioSource.PlayOneShot(successSound);
print("Should have played ding!");
Invoke("LoadNextLevel", 1f); //Delay loading of next scene by 1 second.
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(deathSound);
Invoke("LoadFirstLevel", 1f); //Delay loading of next scene by 1 second.
}
private void LoadNextLevel()
{
SceneManager.LoadScene(1); //fix for multiple levels
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audioSource.Stop();
}
}
private void ApplyThrust()
{
float thrustThisFrame = mainThrust * Time.deltaTime;
rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true; //Take manual control of rotation
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false; //Return control of rotation to Unity physics
}
}
Console Output: