Audio.PlayOneShot() when put in the OnCollisionEnter message

My approach to solve this problem was to simply use the State of the rocket launcher to play the desired sound and that\s the only thing that actually worked.

void Update()
    {
        ProcessInput();
    }
private void ProcessInput()
    {
        if (state == State.Alive)
        {
            RespondToThrust();
            RespondToRotate();
        }
        else if(state == State.Dying)
        {
            if (!GetComponent<AudioSource>().isPlaying) GetComponent<AudioSource>().PlayOneShot(Deathaudio);
        }
        else if(state == State.Transcending)
        {
            if (!GetComponent<AudioSource>().isPlaying) GetComponent<AudioSource>().PlayOneShot(Winaudio);
        }
        else GetComponent<AudioSource>().Stop();
    }

When i used to the same as the video which was to include it in the OnCollisionEnter Message, no sounds were played on collision, neither the death nor the winning sound.

Here’s my first try:

private void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) return;

        if(collision.collider.tag == "Win")
        {
            Audio.Stop();
            GetComponent<AudioSource>().PlayOneShot(Winaudio);
            print("Winning");
            state = State.Transcending;
            Invoke("LoadNextLevel", 1f);
        }
        else if (collision.collider.tag != "Friendly")
        {
            Audio.Stop();
            GetComponent<AudioSource>().PlayOneShot(Deathaudio);
            print("DYING");
            Invoke("ReloadLevel", 1f);
            state = State.Dying;
        }
    }

Thanks in advanced for reading and i appreciate the help ")

Good job, Eloi! :slight_smile:

1 Like