Two key input at the same time

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

if i press left and right key at the same time, positive and negative positions neutralize each other. and character wil stop. i want control like Atari 2600 River Raid movement . If two keys are pressed at the same time, select the last one pressed . River raid is designed for joystick. I’m talking about Stella emulator controls. The javascript version has similar controls but slightly different → River Raid (Atari 2600) - online game |
How can i do with Gdscript? thanks.

extends KinematicBody2D

var speed = 400
var acceleration = 0.1
var velocity = Vector2.ZERO
var input_velocity = Vector2.ZERO

func _physics_process(delta):
	input_velocity = Vector2.ZERO
	if Input.is_action_pressed("ui_right"):
		input_velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		input_velocity.x -= 1

	input_velocity = input_velocity.normalized() * speed

	if input_velocity.length() > 0:
		velocity = velocity.linear_interpolate(input_velocity, acceleration)

		velocity = Vector2.ZERO
	velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: jgodfrey

Here’s an example of one possibility. Just season to taste…

extends Node2D
var x_delta = 0

func _physics_process(delta):
	var input_velocity = Vector2.ZERO

	if Input.is_action_just_pressed("ui_right"):
		x_delta = 1

	if Input.is_action_just_pressed("ui_left"):
		x_delta = -1

	if Input.is_action_just_released("ui_right") && x_delta > 0:
		x_delta = 0

	if Input.is_action_just_released("ui_left") && x_delta < 0:
		x_delta = 0

	input_velocity.x = x_delta