- Home /
Duplicate Question, asking for script
Help with Simple Footsteps?
Hello,
Can anybody help me create a Simple Footstep script? I have tried writing my own and they work, but they are glitchy. I don't need/want multiple footstep sounds, so really I just a need an audio source to play and stop when I use the "WASD" keys. If anybody could help me out with this, I would really appreciate it! I've been stuck on this for a while, and I would LOVE some help! Thank you for your time!
Did the one I sent you from the other question not help?
$$anonymous$$aybe this tutorial would help you: https://www.youtube.com/watch?v=_cJR6QQ9T94
The other one your sent me totally worked! The only problem was whenever I hit "A S or D" when I was already holding W (strafing), The sound would restart. Any ideas on how to make it stay playing even if you hit another key? Thanks for your help, man! I seriously appreciate it!
Ah alright I would check out drak8888's answer on your other question he said some good stuff about using Horizontal and Vertical inputs that might work out better. Wish I could help out more!
Sounds awesome! That's actually exactly what I am working on as well! Yeah please send me a link :D Well without further ado here is the code I put together:
#pragma strict
@script RequireComponent(AudioSource)
var AudioFile : AudioClip;
var controller : CharacterController;
var player : GameObject;
var vectorZero : Vector3;
function Start(){
player = GameObject.Find("Player");
controller = player.GetComponent(CharacterController);
vectorZero = Vector3.zero;
}
function Update()
{
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if(controller.velocity == vectorZero){
audio.Play();
}
}
else if (Input.GetAxis("Horizontal") == 0 || Input.GetAxis("Vertical") == 0)
{
if(controller.velocity != vectorZero){
audio.Stop();
}
}
}
This should work with $$anonymous$$imal tweaking. I had an audio source attached to my player with this script. Also, I had loop checked.
Answer by Zmiho · Nov 23, 2013 at 09:11 PM
Make Javascript called FootStepsOn:
var AudioFile : AudioClip;
function Update() {
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
}
Then make Javascript called FootStepsMute:
var AudioFile : AudioClip;
function Update() {
if (Input.GetKeyUp (KeyCode.W))
{
audio.clip = AudioFile;
audio.Stop();
}
}
This will work and thank you! But will no audio play when I use the other walk keys such as "A" "S" and "D?" For example, if the player walks backwards, won't it be silent? Do you know of any way around this?
just add something like this:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A))
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.S))
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D))
and for mute the same but .Get$$anonymous$$eyUp
But this will make the audio restart if I walk sideways, won't it? Is there anyway to make it not restart?
Answer by Jellezilla · Nov 23, 2013 at 09:10 PM
Haven't tried it myself but here's my two cent:
If it is for a FPS or similar, where you can't see the feet it should just be something like: if(isWalking) { Footsteps.Play(); }
This however, will not work if it needs to be synchronized with the graphics. Gl :)
Thank you! Yes, it is an FPS so your first approach should work! I'm just trying to make it so the audio plays no matter which direction (or button) the player hits.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Footstep Script Not Working 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Simple footsteps? 1 Answer
Audio source in two different scenes 1 Answer