Eggys Games

Sound Variance

Posted on by Eggy

So a neat little trick when adding sound in games is randomising the pitch so one sound effect turns into 10. This means the user will always here a slightly different tone and it makes it feel like their is more sound effects added and gives it a more realistic effect. The volume is also using Unitys audio listeners to make the sound volume lower when you are further away, giving a proper ambience. The code below shows this, you'll also notice the sound doesn't activate unless the object is hit at a higher speed, this is good for only activating the sound effect when you push it over or it falls rather then simply touching it gently. 

We also use a Collision Function.

void OnCollisionEnter2D (Collision2D col) {

        if(col.gameObject.tag == "Ground") {

            if (col.relativeVelocity.magnitude > 4) {
                myrandom = Random.Range (0.5f, 1.5f);


                Soccersound.pitch = myrandom;
                Soccersound.Play ();
            }

        }
}