Bad input behavior

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rredesigns
:warning: Old Version Published before Godot 3 was released.

Hello.

I have a weird problem. I’m using the following code to move a character in the 3d engine, and changing the animation too.
Only the spacebar actually works. For some reason, the other keys won’t change the value of “anim”, and therefore the animation player won’t change either.
Please ignore the repeated “Walk” assignments, they are just there as I was trying with all keys.

Previously, “anim” and “anim_next” were declared as empty strings. I’m using Godot 2.0 stable.

Thanks in advance.

if anim != anim_next:
	print(anim)
	anim_next = anim
	anim_player.play(anim)	


if Input.is_action_pressed("forward"):
	anim = "Walk"
	set_linear_velocity(Vector3(speed, get_linear_velocity().y, get_linear_velocity().z))
	
	
if Input.is_action_pressed("backwards"):
	set_linear_velocity(Vector3(-speed, get_linear_velocity().y, get_linear_velocity().z))
	anim = "Walk"
	
if Input.is_action_pressed("left"):
	set_linear_velocity(Vector3(get_linear_velocity().x, get_linear_velocity().y, -speed))
	anim = "Walk"
	
if Input.is_action_pressed("right"):
	set_linear_velocity(Vector3(get_linear_velocity().x, get_linear_velocity().y, speed))
	anim = "Walk"
	
if Input.is_action_pressed("btn_jump"):
	set_linear_velocity(Vector3(get_linear_velocity().x, speed, get_linear_velocity().z))
	anim = "Walk"
	
else:
	anim = "Run"
:bust_in_silhouette: Reply From: volzhs

All other if were ignored because of last if else statement.
Let’s assume “backwards” is pressed.

anim is changed to “Walk” by first if Input.is_action_pressed(“backwards”):
but it’s changed again to “Run” because of last if else.

I guess you shoude use elif instead of just if.

It does work using “elif”. Now the reason I didn’t use it in the first place, is because I can’t get the input from more than one key at a given time (to move diagonally).

Probably, it would be best to just separate the input in different functions, but, will that work as I need it to?

rredesigns | 2016-02-26 03:05