Almost every game that uses "WASD" keys for char navigation, use "A + A" and "D + D" key combinations for running left and right.
So my character is able to walk but I don't see any good running implementation in Godot.
How to implement playing of running animation when user is pressing one key twice?
upd: why it's not working?
extends KinematicBody2D
export var move_speed: float = 200
var velocity = Vector2.ZERO
var run_state = false
func test():
var turned_right = velocity.x > 0
var turned_left = velocity.x < 0
if turned_right && run_state == false:
$AnimatedSprite.animation = "walk"
var run_state = true
elif turned_right && run_state == true:
$AnimatedSprite.animation = "run"
func _physics_process(delta):
var input_direction = Vector2(
Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up"))
velocity = input_direction.normalized() * move_speed
if velocity.length() > 0:
$AnimatedSprite.play()
else:
$AnimatedSprite.play("idle")
test()
move_and_slide(velocity)
I fixed everything myself.
Here's the working code:
extends KinematicBody2D
export var speed = 200
var run_state = false
func _process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.play("idle")
position += velocity * delta
var turned_right = Input.is_action_just_pressed("ui_right")
if turned_right && run_state == false:
$Timer.start()
$AnimatedSprite.animation = "walk"
$AnimatedSprite.flip_h = false
run_state = true
elif turned_right && run_state == true:
$Timer.start()
$AnimatedSprite.animation = "run"
$AnimatedSprite.flip_h = false
run_state = false
func _on_Timer_timeout():
run_state = false
Still have no clue why the code above didn't work. And this way of doing a simple tasks like "D + D" is pain in ass