- Home /
Play audio clip when grounded...?
I want to play a audioclip when the character is touching the ground and I want to pause the audioclip when the character is in the air. Okay, so I have this script attached to a gameobject with an audiosource:
var controller : CharacterController;
function Update ()
{
if (controller.isGrounded)
{
audio.Play();
}
else
{
audio.Pause();
}
}
But I can't get it to work. All this does is start a static noise.
I would be grateful if somebody would try help me.
Thanks in advance.
Answer by DaveA · Sep 02, 2011 at 10:04 PM
You're hitting Play each frame. Better to have a boolean, something like (untested):
var wasGrounded = false;
function Update()
{
if (controller.isGrounded && !wasGrounded) // just hit the ground
audio.Play();
else if (wasGrounded && !controller.isGrounded) // just left the ground
audio.Pause();
wasGrounded = controller.isGrounded;
}
Great. Now please 'accept' the answer by hitting that checkmark to the left there near the thumbs up/down and vote count. Cheers
$$anonymous$$ake sure to also set "wasGrounded" to false when you aren't grounded or the sound will only play once
Dont forget to check the Loop-Box in the Inspector on the Audio-Source
Your answer
![](https://koobas.hobune.stream/wayback/20220612200744im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Does the -OnTriggerStay collider- script have to be child of collider triggering the function? 2 Answers