0 votes

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 -> https://www.retrogames.cz/play_036-Atari2600.php?language=EN
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)
in Engine by (163 points)
edited by

1 Answer

0 votes
Best answer

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

by (19,302 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.