- Home /
Question by
Rodrigo Cordova · Oct 12, 2014 at 07:02 PM ·
javascriptinput.getaxisgroundedsound on key
Stop walk sound when not grounded?
Hey guys, i need your help for a code, i have my script for FootSteps sound, works great, but i have a little issue, when i jump and walk at the same time, the sound don't stop, i need it to play only the on ground, can you guys help me?
#pragma strict
//Set your variable for the footstep audioclips
var footstepsWood : AudioClip;
var footstepsMetal : AudioClip;
var footstepsStone : AudioClip;
var character : CharacterController;
//This script requires that an audio source component is attached to your First Person Controller
@script RequireComponent(AudioSource);
function Awake(){
character = GetComponent(CharacterController);
}
function LateUpdate ()
{
//Check whether the user is moving the player
if(character.isGrounded){
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
{
// if the audio is not playing then play - this stops it playing multiple clips at thye same time
if (!audio.isPlaying)
{
audio.Play();
}
}
else
{
audio.Stop();
}
}
}
function OnControllerColliderHit (hit : ControllerColliderHit)
{
// if your character controller is on a floor piece tagged as woodFloor then it sets the audioClip as footstepsWood and sets the volume to 90%
if (hit.gameObject.tag == "woodFloor")
{
audio.clip = footstepsWood;
audio.volume = 1.0;
}
else if (hit.gameObject.tag == "metalFloor")
{
audio.clip = footstepsMetal;
audio.volume = 1.0;
}
else if (hit.gameObject.tag == "stoneFloor")
{
audio.clip = footstepsStone;
audio.volume = 1.0;
}
}
Comment
Answer by Asthegor · Oct 12, 2014 at 07:29 PM
Hi,
I'm a beginner on Unity (1 month only). But, have you check the value of your boolean "character.isGrounded" ? I had a issue which look like that and my boolean was always at "false".
Your answer
![](https://koobas.hobune.stream/wayback/20220613164944im_/https://answers.unity.com/themes/thub/images/avi.jpg)