Godot Version
2.4.1.stable
Question
I can’t move diagonally with this code but I want to. Please help. I’m a beginner so sorry if this is a bad question.
extends CharacterBody2D
@onready var animations = $AnimationPlayer
@onready var sprite= $Sprite2D
@export var speed: int = 100
var current_dir = "none"
func playerMovement(delta):
if Input.is_action_pressed("ui_right"):
current_dir = "right"
playAnimations(1)
velocity.x = speed
velocity.y = 0
elif Input.is_action_pressed("ui_left"):
current_dir = "left"
playAnimations(1)
velocity.x = -speed
velocity.y = 0
elif Input.is_action_pressed("ui_down"):
current_dir = "down"
playAnimations(1)
velocity.x = 0
velocity.y = speed
elif Input.is_action_pressed("ui_up"):
current_dir = "up"
playAnimations(1)
velocity.x = 0
velocity.y = -speed
else:
playAnimations(0)
velocity.x = 0
velocity.y = 0
func playAnimations(movement):
var dir = current_dir
if dir == "right":
sprite.flip_h = false
if movement == 1:
animations.play("walkRight")
elif movement == 0:
animations.play("idleRight")
if dir == "left":
sprite.flip_h = true
if movement == 1:
animations.play("walkRight")
elif movement == 0:
animations.play("idleRight")
if dir == "down":
sprite.flip_h = true
if movement == 1:
animations.play("walkDown")
elif movement == 0:
animations.play("idleDown")
if dir == "up":
sprite.flip_h = true
if movement == 1:
animations.play("walkUp")
elif movement == 0:
animations.play("idleUp")
func _physics_process(delta):
playerMovement(delta)
move_and_slide()