- Home /
Play noise when landing on the ground unity c#
I have a player object using a character controller and a capsule collider.
I have a ground object that is made up of sveral cubes (as platforms) that each have a box collider.
The ground has a Rigidbody.
The audio plays fine when jumping around on the first block but when attempting to land on the second as the player approaches the ground a spam of noise occurs(the audio clips being played multiple times at the same time).
All of my collision boxes are setup to the correct size but the noise occurs before the player touches the collision box or the ground?
Here is what I have so far:
[RequireComponent(typeof(AudioSource))]
public class Landing : MonoBehaviour
{
public AudioClip[] list;
public AudioClip pl_wade1;
public AudioClip jumplanding4;
AudioSource audio;
CharacterController characterController;
int number;
void Start ()
{
audio = GetComponent<AudioSource>();
characterController = GetComponent<CharacterController>();
//Loading the items into the array
list = new AudioClip[]
{
(AudioClip)Resources.Load("Sound/jumplanding1"),
(AudioClip)Resources.Load("Sound/jumplanding2"),
(AudioClip)Resources.Load("Sound/jumplanding3"),
(AudioClip)Resources.Load("Sound/jumplanding4")
};
}
void OnCollisionEnter (Collision col)
{
int number;
number = Random.Range (0, list.Length);
if (col.gameObject.name == "Ground")
{
if(!audio.isPlaying)
{
audio.PlayOneShot(list[number], 0.1f);
}
}
}
}
Hi. I would suggest changing your question title to a more accurate one, as you already know how to play a noise when landing on the ground. $$anonymous$$aybe a subject like "Prevent audio instances from occurring multiple times in 1 OnCollisionEnter" or "prevent collider from registering multiple times per frame".
Answer by DanGoldstein91 · May 21, 2015 at 06:39 AM
I ran into this same issue last month, I used a bool to only allow sounds to fire off once per collision. I'm not sure what audio.isPlaying does so I added my bool alongside it. I think this might solve your problem:
void OnCollisionEnter (Collision col)
{
int number;
number = Random.Range (0, list.Length);
bool canPlayJumpLandingSound;
if (col.gameObject.name == "Ground")
{
canPlayJumpLandingSound = true;
if(!audio.isPlaying && canPlayJumpLandingSound == true)
{
canPlayJumpLandingSound = false;
audio.PlayOneShot(list[number], 0.1f);
}
}
}
If for some reason that still doesn't work, you can start a coroutine that only plays sound and enables canPlayJumpLandingSound after a Yield return new WaitForSeconds (0.02f). It would look something like this:
void OnCollisionEnter (Collision col)
{
int number;
number = Random.Range (0, list.Length);
if (col.gameObject.name == "Ground")
{
canPlayJumpLandingSound = true;
if(!audio.isPlaying && canPlayJumpLandingSound == true)
{
StartCoroutine(PlayLandingSound())
}
}
}
IEnumerator PlayLandingSound ()
{
canPlayJumpLandingSound = false;
audio.PlayOneShot(list[number], 0.1f);
yield return new WaitForSeconds (0.02f)
canPlayJumpLandingSound = true;
}
This coroutine was my plan B as a workaround for the spamming on contact. I tested it and it worked great with a delay of 0.02f, but further testing showed the bool by itself got the job done in my case. Hope that helps.
Your answer
Follow this Question
Related Questions
Why is my score not being updated when the collision happens? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Playing audio clip 2 Answers
Object following a path and colliding with other objects with physics. 1 Answer