Odd behaviour in playing death & success sounds

Hello there,
I’ve noticed this odd behavior, despite my code matching that of the resources, but went through and finished the project in hope this bug would be later identified. No luck.

The thrusting sound works perfectly. It appears somehow that the “death” and “win” sounds only play if I die/win while hitting the space bar; i.e., while thrusting. Otherwise, it is silent. The particle effects are working without issues although they are in the same method.

Again, I figured this would be a bug due to Unity version 2018.4.13f1. However, I loaded the project from Udemy resources and the sounds seem to work just fine there, although the code is the same.
Any clues there?

My project is enclosed.
3_Project_Boost (EM).zip (513.8 KB)

Regards,
M

Hi Makkia,

Could you please share your Rocket script as formatted text here in your thread?


See also:

Sure, here it is
‘’’
using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{

Rigidbody rigidBody;
AudioSource audioSource;

[SerializeField] float rcsThrust = 90f;
[SerializeField] float mainThrust = 20f;

[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip explosion;
[SerializeField] AudioClip win;

[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem explosionParticles;
[SerializeField] ParticleSystem winParticles;

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

bool debugDisabled = true;

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

// Update is called once per frame
void Update()
{
    RespondToThrustInput(); 
    RespondToRotateInput();

    if (Debug.isDebugBuild)
        RespondToDebug();
}

private void OnCollisionEnter(Collision collision)
{

    if (state != State.Alive || ! debugDisabled) return; // ignore collissions

    switch (collision.gameObject.tag)
    {
        case "Friendly":
            print("OK");
            break;
        case "Finish":
            StartSuccessSequence();
            break;
        default:
            StartDeathSequence();
            break;
    }
}

private void StartSuccessSequence()
{
    state = State.Trans;
   
    audioSource.Stop();
    audioSource.PlayOneShot(win);
    winParticles.Play();
    Invoke("LoadNextScene", 1f);
}

private void StartDeathSequence()
{
    state = State.Dying;
    
    audioSource.Stop();
    audioSource.PlayOneShot(explosion);
    explosionParticles.Play();
    Invoke("ReLoadScene", 1f);
}

private void ReLoadScene()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

private void LoadNextScene()
{
    SceneManager.LoadScene(1); //allow for more levels
}

private void RespondToThrustInput()
{
    if (Input.GetKey(KeyCode.Space)) //Can Thrust while rotating
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();
        mainEngineParticles.Stop();
    }
}

private void ApplyThrust()
{
    rigidBody.AddRelativeForce(Vector3.up * mainThrust);
    if (!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
        mainEngineParticles.Play();
    }
}

private void RespondToRotateInput()
{

    rigidBody.freezeRotation = true; //halt the physics to improve control
    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; // continue physics
}
 private void RespondToDebug()
{
    if (Input.GetKeyDown(KeyCode.C))
        debugDisabled = !debugDisabled; // toggle debug
    else if (Input.GetKeyDown(KeyCode.L))
        LoadNextScene();
}

}
‘’’

Thank you. The problem is very likely here:

private void RespondToThrustInput()
{
    if (Input.GetKey(KeyCode.Space)) //Can Thrust while rotating
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();
        mainEngineParticles.Stop();
    }
}

The else block gets executed if the if-condition was not evaluated to true.

What you could do is to wrap the RespondToThrustInput() and RespondToRotateInput() method calls in Update into an if block. The condition would be the state, which should be State.Alive.

Please test that. Did it fix the issue?

1 Like

Yess!!1 Super thanks Nina, this solved the problem. Finally I can hear the jiggle sound.
Indeed, the RespondToThrustInput was being executed at all times and when I am not Thrusting, sound would stop. Didn’t catch that before. Thanks again.

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

Privacy & Terms