I want my Jump sound play one time when jumping (230332)

I Can Play The Jump Sound Multiple Times While My Character Is Still In The Air And I Just Want The Character To Make The Jump Noise Only When He Is On The Ground.
Here Is The Code

public AudioSource jump;

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
        jump.Play();
}

}

AudioSource component is with loop attribute enabled?

–

Why not just add a boolean flag? public AudioSource jump; private hasPlayedJump; void Update() { if (Input.GetKeyDown(KeyCode.Space)) { jump.Play(); hasPlayedJump = true; } else hasPlayedJump = false; } This should work on space key down. You should also add (&&) another condition to that to see if your player is grounded. I recommend you look at the exact code you used in a Player movement script, check the conditions that make the player jump, and copy those.

–

1 Answer

1