- Home /
Scripting Walking Sound Help?
Hey All ! I am working on a school project but I am an art focused student, so bare with me.
I have this script to play footsteps when our character walks:
public class Walking : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if ( Input.GetButton( "Horizontal" ) || Input.GetButton( "Vertical" ) )
audio.Play();
else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && audio.isPlaying )
audio.Stop(); // or Pause()
}
}
I have quite a few questions:
1)It works just fine, however we are using a controller for our game and this only works with the keyboard. I tried GetAxis but that doesn't work.
2) She still has the walking sound when she is jumping in the air. I have seen things about isGrounded, but I haven't found anything in depth enough on an explanation, so I don't understand how to add it to this code.
3) I might need to change the code so that a different noise will play when the character jumps and does other actions, so I have to know how to call on specific audio sounds. Would I do this playing the PlayOneShot (filename here)?
Thank you for any help! Again, I apologize that I am not one versed in codes and scripts.
Answer by Tanshaydar · Nov 14, 2014 at 08:31 AM
1) Instead of GetButton
, use GetAxis
2) If your character controller has a rigidbody and collider components, you can do raycast testing for isGrounder part. Sample Assets have a nice Rigidbody FPS controller and code in it.
3) I'd use different arrays of sounds for each material sound, and depending on material type I'd switch between arrays to play sounds, but that's one way to do it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171447im_/https://answers.unity.com/themes/thub/images/avi.jpg)