Attention | Topic was automatically imported from the old Question2Answer platform. | |
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