[Solved] Question on audioSource.Stop()

Hello! Asking this to reframe my thinking; since the entire AudioSource component for the rocket is “redirected” to audioSource, all sound coming from the rocket should cut when audioSource.Stop() is used, shouldn’t it? From the following code, it looks to me like I shouldn’t hear ANYTHING from the rocket when space bar isn’t pressed:

void RespondtoThrustInput() {
    if(Input.GetKey(KeyCode.Space)) {
        Thrust();
    } 
    else {
        audioSource.Stop();
    }
}

void Thrust() {
    float thrustThisFrame = mainThrust * Time.deltaTime;
    rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame);
    if(!audioSource.isPlaying) {
        audioSource.PlayOneShot(mainEngine);
    }
}

…and yet I hear the victory and defeat sounds when not holding space bar. How exactly does audioSource.Stop() function? Thank you for your time!

Hi Cole,

Does the RespondtoThrustInput get called when the victory or defeat sounds are being played? The Thrust method does not have anything to do with those two sounds.

The code works, and victory/defeat aren’t calling Thrust(), but it looks to me like if spacebar isn’t held down, the audioSource on the rocket should be completely shut off:

void RespondtoThrustInput() {
if(Input.GetKey(KeyCode.Space)) {
Thrust();
}
else {
audioSource.Stop();
}
}

My problem is I don’t understand why I can hear victory and defeat sounds when I’m not holding spacebar. I WANT to hear those and I do, but my problem is just the understanding.

Do you play the victory and defeat sounds with PlayOneShot? If so, the Stop method won’t affect it. See this answer on stackoverflow.

1 Like

Awesome, thank you!

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

Privacy & Terms