Avoid holding down two buttons at the same time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EternalQuestioner

I have a 2D game with jump (ui up), walk right (ui right), and walk left (ui left) buttons, everything works fine but when I press “ui right” and “ui left” the character stays static, any way to disable one of the buttons to avoid these errors? thanks in advance

extends KinematicBody2D

export var speed = 200
export var gravity = 750
export var jump = 400

var distance = Vector2()
var velocity = Vector2.ZERO
var direction_x = 0

func _ready():
set_physics_process(true)


 func _physics_process(delta):

direction_x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))

if is_on_floor():	
	if Input.is_action_just_pressed("ui_up"):
	#	velocidad.y = 0
		velocity.y = -jump
		

distance.x = speed*delta
velocity.x = (direction_x*distance.x)/delta
velocity.y += gravity*delta

velocity = move_and_slide(velocity, Vector2.UP)

Certainly, but it comes down to how your code handles the input events. Can you show that code?

jgodfrey | 2022-10-07 19:34

Thanks for answering, I already updated the question but I’ll write you the code anyway:

extends KinematicBody2D

export var speed = 200
export var gravity = 750
export var jump = 400

var distance = Vector2()
var velocity = Vector2.ZERO
var direction_x = 0

func _ready():
set_physics_process(true)


 func _physics_process(delta):

direction_x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))

if is_on_floor():	
	if Input.is_action_just_pressed("ui_up"):
	#	velocidad.y = 0
		velocity.y = -jump
		

distance.x = speed*delta
velocity.x = (direction_x*distance.x)/delta
velocity.y += gravity*delta

velocity = move_and_slide(velocity, Vector2.UP)

EternalQuestioner | 2022-10-07 20:27

So, the main issue is here:

direction_x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))

That’ll return 0 if both left and right are pressed simultaneously which, ultimately, will generate no motion. What do you want to happen when both keys are pressed? Really, “no motion” isn’t at all unusual in this situation.

jgodfrey | 2022-10-07 20:41

:bust_in_silhouette: Reply From: alexp
if Input.is_action_pressed("ui_right"):
  direction_x = 1
elif Input.is_action_pressed("ui_left"):
  direction_x = -1

This will favor right over left if you have them both pressed. If you want to favor the most recent direction that was pressed, you should use events.

var fresh_direction
func _input(event):
  if event.is_action_pressed("ui_right"):
    fresh_direction = 1
  elif event.is_action_pressed("ui_left"):
    fresh_direction = -1

func _physics_process(delta):
  var press_right = Input.is_action_pressed("ui_right")
  var press_left = Input.is_action_pressed("ui_left")
  if press_right && press_left:
    direction_x = fresh_direction
  elif press_right || press_left:
    direction_x = press_right - press_left
  else:
    direction_x = 0