[Javascript] s there Operator OR?

Hello!

Im making footstep stuff in my game and i try get OR operator working with javascript. And having some problems.

Idea is that i have audiosource object attached on player and i manipulate key hits with my inputmanager.

i have this code

	if(Input.GetKeyDown("w"||"s"||"a"||"d"){
		WalkSoundToggleOn();
	}

	if(Input.GetKeyUp("w"||"s"||"a"||"d")){
		WalkSoundToggleOff();
	}

inside update function. yes i know there is simple way to do this but i wanna save some lines and optimize this script so far i can.

in javascript “||” should be OR operator and “” AND. But looks like it dosent work. it only works when i press w to walk forward.

Yes, i solved it

	if(Input.GetKeyDown("w") || Input.GetKeyDown("s")|| Input.GetKeyDown("a")|| Input.GetKeyDown("d")) {
		WalkSoundToggleOn();
	}
	
		if(Input.GetKeyUp("w") || Input.GetKeyUp("s")|| Input.GetKeyUp("a")|| Input.GetKeyUp("d")) {
		WalkSoundToggleOff();
	}

Only think now is that someway happens that no sound play at all… dunno why … yet

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

your logic also doesn’t work. If you press w down, then press a and release he sound will stop and he will still be moving.

TheRaider yes you are right, that was just my speudo thingin of how i do it. Dreamora thanks for answer, i try figure this out.

Looks like im lost… or i just make thinks to complicated. Any tips?

What about something like

if(keys pressed) //use the one which checks if keys are currently being pressed, not just down or up
WalkSoundToggleOn();
else
WalkSoundToggleOff();

using your logic

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

well if you want to use the GetKeyDown for speed reasons use the same thing except have a counter which tracks the keypresses and keypress ups.

so make moving false just
if (Input.GetKey(“w”))
Moving = true;
else
Moving = false;

so it is false whenever the key isn’t being pressed.

If you wrote your own controller you can just put a global variable in the movement loop to keep track that way.

Yeah that works, but some reason

	if(Moving == true){
		WalkSoundToggleOn();
	}
	else{
		WalkSoundToggleOff();
	}

wont let me hear foot stepping,

but

	if(Moving == true){
		WalkSoundToggleOn();
	}
	else{
	}

Works, but now we dont have anything what could end looping.

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.

I will post some code from those functions when i get home.

Okey here you go

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();
}

Okey, after many hours work and help from my friend. We succes this.

Just add audiosource to the same object with this script and point audioInput to that:

var AudioInput : AudioSource;
function Update(){
	if (Input.GetAxis("Vertical") > 0.2 || Input.GetAxis("Vertical") < -0.2) {
		WalkSoundToggleOn();
	}
	else {
		if (Input.GetAxis("Horizontal") > 0.2 || Input.GetAxis("Horizontal") < -0.2) {
		}
		else{
			WalkSoundToggleOff();
		}
	}
	
	if (Input.GetAxis("Horizontal") > 0.2 || Input.GetAxis("Horizontal") < -0.2) {
		WalkSoundToggleOn();
	}
	else {
		if (Input.GetAxis("Vertical") > 0.2 || Input.GetAxis("Vertical") < -0.2) {
			
		}
		else{
			WalkSoundToggleOff();
		}
	}
}

function WalkSoundToggleOn()
{
	if(AudioInput.audio.isPlaying) {
	}
	else {
		AudioInput.audio.Play();
	}
}

function WalkSoundToggleOff() {
	AudioInput.audio.Stop();
}

I hope this helps some one.

I get the error “Object reference is not set to an instance of an object”. something I’m not linking up properly?

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.

Hope this helps you