- Home /
Can anyone help me modify this simple script?
Hello Everyone,
At the current moment, I am working on making footstep audio. The script I have basically says to play audio when the "W" key is held down, and stop when it is let go. This script works perfectly, but the only problem is if you walk sideways or backwards, the audio doesn't play... Basically, I'm wondering if anybody can help me modify this script so that it has multiple keycodes for "W" "A" "S" and "D." If anybody could help me out with this, I wouls REALLY appreciate it! Thank you so much for your time!
#pragma strict
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.W))
{
audio.Stop();
}
}
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
Answer by TheLivingTree · Nov 23, 2013 at 05:29 PM
If you just want to extra have key presses try something like this:
#pragma strict
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.D))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.S) || Input.GetKeyUp (KeyCode.D))
{
audio.Stop();
}
}
//Why are you repeating this step? It isn't necessary
//if (Input.GetKeyDown (KeyCode.W))
// {
// audio.clip = AudioFile;
// audio.Play();
// }
The '||' symbol is considered 'or' in logic. So the code is saying in english: if the w or a or d or s key is pressed execute this code. However, rather than use Input.GetKey I would suggest [Input.GetButton][1]. This allows for more flexibility because a menu can be made to change the input mid game. I would highly suggest looking into this, as it will save much time if used early on. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html
$$anonymous$$y pleasure! Thank you for accepting my answer :D
Answer by drak8888 · Nov 23, 2013 at 09:13 PM
Here you go :
#pragma strict
var AudioFile : AudioClip;
function Update()
{
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetAxis("Horizontal") == 0 || Input.GetAxis("Vertical") == 0)
{
audio.Stop();
}
}
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
audio.clip = AudioFile;
audio.Play();
}
This here script will use the Axis instead of the normal button down. An axis is an Input set in your game when you go in Edit > Project Settings > Input.
By default, the axis are set with WASD and the arrow keys, so this should work perfectly.
Also for the sake of understanding, an Axis either gives 1 when positive (W Key) or -1 when negative (S Key), which is why I only check if the Axis is at 0 or not. When at 0, its not pressed and when its not at 0, its pressed. Simple as that.
Answer by clunk47 · Nov 23, 2013 at 10:04 PM
The simplest way, use a footstep sound with a second or two of silence at the end of the clip (however much time you want between footsteps), and do something like:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void Update()
{
if(!audio.isPlaying && (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
audio.Play ();
}
}
Answer by Patrykgazing · Nov 23, 2013 at 05:30 PM
Only way i can help you, is that you can always copy input codes 4 times and only change the KeyCode so it would be like:
#pragma strict
var AudioFile : AudioClip;
function Update()
{
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.W))
{
audio.Stop();
}
else if (Input.GetKeyDown (KeyCode.A))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.A))
{
audio.Stop();
}
else if (Input.GetKeyDown (KeyCode.S))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.S))
{
audio.Stop();
}
else if (Input.GetKeyDown (KeyCode.D))
{
audio.clip = AudioFile;
audio.Play();
}
else if (Input.GetKeyUp (KeyCode.D))
{
audio.Stop();
}
}
It's just my basic solve of this.
Answer by Commander Quackers · Nov 23, 2013 at 05:30 PM
I use this. http://bando.bplaced.net/scripts/Foot%20Steps.js
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Simple footsteps? 1 Answer
Player Footsteps in stairs Problem 1 Answer
Help with Simple Footsteps? 2 Answers