doesn’t play sound cause you don’t allow it. you toggle the sound the whole time on again.
what you would need to do is use getkey to fire it off so it only happens on the first press and then use the getkeyup to stop it again.
either that or if you want to use getkeydown, you would need to handle “is already playing” in the walksoundtoggleon so it does not play the sound if it is already playing it at the time
For using very simple stuff what you told me to do
if(Input.GetKey("w")){
Moving = true;
}
///////////////////////////////////
if(Moving == true){
WalkSoundToggleOn();
}
if(Moving == false){ /// else here, it really dosent matter - does that check it on once or twice.
WalkSoundToggleOff();
}
It wont work, did you think when moving = false? and do you know how MUCH that eats fps? after all sound dosent appear on that style
Maybe it is time to look at your ToggleOff function. Is there something in there which stops the sound running (logically when you run the program that is going to be called until Moving becomes true).
It is kind of wierd cause you should be to hear it is you hold down since you aren’t even calling that function.
Probably good to add some debug functions in your toggleOn and toggleOff to see what is actually being called and when.
function WalkSoundToggleOn()
{
var DragSystem = GameObject.FindGameObjectsWithTag("FootStep");
for (Component in DragSystem)
Component.GetComponent("AudioSource").audio.Play();
}
function WalkSoundToggleOff()
{
var DragSystem = GameObject.FindGameObjectsWithTag("FootStep");
for (Component in DragSystem)
Component.GetComponent("AudioSource").audio.Stop();
}
Solved, I couldn’t get the above code to work properly, but I managed to create a simpler version that runs ok.
This script will allow footsteps audio to be looped as long as at least one key is pressed, and doesnt cut out if multiple are held down and only one released.
var tot : int;
tot=0;
function Update () {
if (Input.GetButtonDown ("Horizontal") ) {
tot++;
audio.Play();
}
if (Input.GetButtonUp ("Horizontal") ) {
tot--;
if (tot == 0) {
audio.Stop();
}
}
if (Input.GetButtonDown ("Vertical") ) {
tot++;
audio.Play();
}
if (Input.GetButtonUp ("Vertical") ) {
tot--;
if (tot == 0) {
audio.Stop();
}
}
}
Version 2 of this code now allows for the sound to keep playing rather than repeating from the begining if the player say uses: " forward, left, forward"
Hope it’s useful to someone.
var tot : int;
tot=0;
function Update () {
if (Input.GetButtonDown ("Horizontal") ) {
if (tot > 0)
tot++;
if (tot < 1) {
tot++;
audio.Play();
}
}
if (Input.GetButtonUp ("Horizontal") ) {
tot--;
if (tot == 0)
audio.Stop();
}
if (Input.GetButtonDown ("Vertical") ) {
if (tot > 0)
tot++;
if (tot < 1){
tot++;
audio.Play();
}
}
if (Input.GetButtonUp ("Vertical") ) {
tot--;
if (tot == 0) {
audio.Stop();
}
}
}
Hi! you must drag and drop your audiosource object to the var AudioInput : AudioSource; Variable on script component. It works and better than GetKeyUp/ Down , because it reads velocity and not keypress. In my option.