I added this exactly as Ben did toward the end of part 47, and now the audio doesn’t start and end as it should. Before I added the ‘else’ statement, the sound was correctly triggered when pressing the spacebar. Now, when I play the game and press the spacebar, the sound doesn’t seem to trigger – it sometimes does, but only after holding down the spacebar for several seconds. Sometimes it will finally trigger, then continue to play for several seconds after I stop pressing the spacebar. There isn’t any consistency in how it’s working. I try pressing and unpressing the spacebar, and it still doesn’t work. At first I thought maybe my system was being a bit laggy but I don’t believe that’s the case. The sound file isn’t very large either. Any ideas?
if (Input.GetKey(KeyCode.Space))
{
rigidBody.AddRelativeForce(Vector3.up);
if (!audioSource.isPlaying)
{
audioSource.Play();
}
else
{
audioSource.Stop();
}