Multiple-audio issues for project boost

I’m having an issue where my audio is not playing for the death or success states but it is having no issues with the thrust noise.
I diffed my cs and the project cs to see what was different to try and figure it out and checked a few other peoples issues out but I couldn’t figure it out.
The only major difference in my script is how I am disabling controls but I don’t think that would affect the audio.

I did try removing the audiosource.Stop(); in the success and death sequence methods but that did nothing.

Any tips or suggestions to try out are most welcome!
I am running on unity 2020.1.16f1.

Thanks :slight_smile:

using UnityEngine;
using UnityEditor.SceneManagement;

public class Rocket : MonoBehaviour
{
    Rigidbody rigidbody;
    AudioSource audioSource;

    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 25f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip deathSound;
    [SerializeField] AudioClip loadingSound;

    bool enableControls = true;

    enum State { Alive, Dying, Trancending }
    State state = State.Alive;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        ProcessInput();
    }

    private void ProcessInput()
    {
        Thrust();
        Rotate();
    }

    void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "Friendly":
                break;
            case "Finish":
                SuccessSequence();
                break;
            default:
                DeathSequence();
                break;
        }
    }

    private void DeathSequence()
    {
        state = State.Dying;
        enableControls = false;
        audioSource.Stop();
        audioSource.PlayOneShot(deathSound);
        Invoke("DeathScene", 2f);
    }

    private void SuccessSequence()
    {
        state = State.Trancending;
        enableControls = false;
        audioSource.Stop();
        audioSource.PlayOneShot(loadingSound);
        Invoke("LoadNextScene", 2f);
    }

    private void LoadNextScene()
    {
        EditorSceneManager.LoadScene(1); //Allow for increment levels
    }

    private void DeathScene()
    {
        EditorSceneManager.LoadScene(0);
    }


    private void Rotate()
    {
        rigidbody.freezeRotation = true; //Take manual contorl of rotation
        float rotationThisFrame = rcsThrust * Time.deltaTime;

        if (Input.GetKey(KeyCode.A) && enableControls == true)
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
            print("A key pressed");
        }
        else if (Input.GetKey(KeyCode.D) && enableControls == true)
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
            print("D key pressed");
        }

        rigidbody.freezeRotation = false; //Resume physics roation
    }

    private void Thrust()
    {
        if (Input.GetKey(KeyCode.Space) && enableControls == true ) //can thrust while rotating
        {
            ResponToThurst();
            print("space key pressed");
        }
        else
        {
            audioSource.Stop();
        }
    }

    private void ResponToThurst()
    {
        rigidbody.AddRelativeForce(Vector3.up * mainThrust);
        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngine);
        }
    }
}

Hi,

Welcome to our community! :slight_smile:

Have you already tried to increase the delay value in the Invoke methods? Or to comment out the lines with Invoke for testing purposes? The LoadScene method does not wait until the audio source completed playing the sound.

1 Like

I am not sure if this will fix it but here is what I did and it is working for me:

[SerializeField] AudioClip successSound;
     private void HandleFinish()
    {
        currentState = State.Transcending;
        HandleSuccessSound();
        Invoke("LoadNextScene", waitTime);
    }

    private void HandleSuccessSound()
    {
        starShipBooster.Stop();
        starShipBooster.PlayOneShot(successSound);
        if (!starShipBooster.isPlaying)
        {
            starShipBooster.PlayOneShot(successSound);
        }
    }

 private void HandleHit()
    {
        HandleHitSound();
        shipHealth -= healthLost;
        currentState = State.Dead;
        Invoke("LoadOnDeath", waitTime);
    }
    
    private void HandleHitSound()
    {
        starShipBooster.Stop();
        starShipBooster.PlayOneShot(collisionSound);
        if (!starShipBooster.isPlaying)
        {
            starShipBooster.PlayOneShot(collisionSound);
        }
    }

So pretty much I just check to make sure it is not playing before so it does not cause a bug. I am not sure if that is the cause of the error though, but my sound effects are working so that might the issue.

1 Like

Hey @Nina, thanks for the welcome! I had a go adding debug lines everywhere and commenting out bits of code and checking things out, after @Radrockstar98 suggestion I split things up a bit more and noticed that I wasnt handling my states correctly, namely I was not declaring if (state == State.Alive).

So I went through everything again and ensured states we’re being handled correctly, removed my method of disabling controls and it seemed to now work!

Thanks for the help / tips :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms