Different way to stop the audio. Is it ok? Seems to work

Hi,

I used a different way to stop the audio, seemed correct to me but not the same as Ben’s. Anyone else get this way or know if it’s the wrong way to do it?

    if (Input.GetKey(KeyCode.Space)) //can thrust whilst rotating
    {
        rigidBody.AddRelativeForce(Vector3.up);
        if (!audioSource.isPlaying) 
        {
            audioSource.Play();
        }
    }
    else if(Input.GetKeyUp(KeyCode.Space))
    {
        audioSource.Stop();
    }

I think your else if statement should look like this … else if(!Input.GetKeyUp(KeyCode.Space)) (with exclamation mark). But as it works…

I believe the original was correct. Input.GetKeyUp returns true exactly once when the key is released. This is the same approach I took and it seems to work. the next time Update() is called, GetKeyUp will return false.

The original question remains - Is there a time when the GetKeyUp approach would fail, when the GetKey approach works?

Oh, I see now that approach should work, :slight_smile:
It may not work though if the code is inside method called outside. Because, there is possible situation, when method with the code is called once and it starts the sound; and then it is simply not called, so it will not detect GetKeyUp (KeyCode.Space).
From Update method should work firne, IMHO.