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.

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;
}