diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-11-14 10:36:23 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-11-14 10:36:23 +0200 |
| commit | bce80184961866e0efcc227becf15fe66bd99428 (patch) | |
| tree | aca6badeaf817f8fa7339cc0b7482d22242d6652 | |
| parent | 5afd1a00a58d53c56ecc003ff0705c0a8773b19f (diff) | |
Fix movement stalling issue when both directions are pressed
| -rw-r--r-- | player.gd | 21 | ||||
| -rw-r--r-- | player.tscn | 5 |
2 files changed, 21 insertions, 5 deletions
@@ -4,6 +4,7 @@ extends CharacterBody2D @export var jump_strength: float var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") +var prev_dir: float = 0.0 # Called when the node enters the scene tree for the first time. func _ready() -> void: @@ -11,7 +12,10 @@ func _ready() -> void: # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta: float) -> void: - $AnimatedSprite2D.flip_h = (velocity.x < 0) + if velocity.x > 0: + $AnimatedSprite2D.flip_h = false + elif velocity.x < 0: + $AnimatedSprite2D.flip_h = true if not is_on_floor(): $AnimatedSprite2D.set_animation("jump") @@ -25,9 +29,18 @@ func _physics_process(delta: float): if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = -jump_strength - - var direction = Input.get_axis("move_left", "move_right") + + var direction: float + if Input.is_action_pressed("move_left") and Input.is_action_pressed("move_right"): + # This is a fix to the stalled movement issue + # where the player stops if both x inputs are pressed at the same time. + # To fix it, we store the previous direction the player was moving in, and change + # the direction when both buttons are held. This is what the player wants nearly always. + direction = -prev_dir + else: + direction = Input.get_axis("move_left", "move_right") + prev_dir = direction + velocity.x = direction * speed - # Move the character move_and_slide() diff --git a/player.tscn b/player.tscn index 077ceb8..4ee3ad4 100644 --- a/player.tscn +++ b/player.tscn @@ -69,7 +69,7 @@ animations = [{ }, { "frames": [{ "duration": 1.0, -"texture": SubResource("AtlasTexture_wtcfe") +"texture": SubResource("AtlasTexture_0e48y") }], "loop": true, "name": &"idle", @@ -110,6 +110,9 @@ sprite_frames = SubResource("SpriteFrames_rj586") animation = &"idle" autoplay = "idle" +[node name="Camera2D" type="Camera2D" parent="AnimatedSprite2D"] +position = Vector2(0, -15) + [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(0, 3) shape = SubResource("RectangleShape2D_onrkg") |
