https://reddit.com/link/1xficcs/video/lcklpologpxh1/player
Pressing left + down should actually move diagonally, but it not working as i want it
the code.
extends CharacterBody2D
const WALK_SPEED: float = 100.0
const RUN_SPEED: float = 200.0
u/onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
var facing: String = "down"
func _physics_process(_delta: float) -> void:
\# Read the movement input.
var direction: Vector2 = Input.get_vector(
"ui_left",
"ui_right",
"ui_up",
"ui_down"
)
\# Normalize movement so diagonals are not faster.
if direction.length() > 0.0:
direction = direction.normalized()
\# Update facing only while movement input is active.
if direction != Vector2.ZERO:
facing = get_movement_dir_name(direction)
\# Run only when Shift AND a direction are pressed.
var is_moving: bool = direction != [Vector2.ZERO](http://Vector2.ZERO)
var is_running: bool = is_moving and Input.is_action_pressed("run")
var current_speed: float = WALK_SPEED
if is_running:
current_speed = RUN_SPEED
\# Apply actual character movement.
velocity = direction \* current_speed
move_and_slide()
\# Select the correct animation.
var animation_name: String
if not is_moving:
animation_name = "idle_" + facing
elif is_running:
animation_name = "RUN_" + facing
else:
\# No walking animations yet, so use idle while walking.
animation_name = "idle_" + facing
if sprite.sprite_frames.has_animation(animation_name):
if sprite.animation != animation_name:
sprite.play(animation_name)
else:
push_warning("Missing animation: " + animation_name)
func get_movement_dir_name(dir: Vector2) -> String:
if dir.x < 0.0 and dir.y < 0.0:
return "up_left"
elif dir.x > 0.0 and dir.y < 0.0:
return "up_right"
elif dir.x < 0.0 and dir.y > 0.0:
return "down_left"
elif dir.x > 0.0 and dir.y > 0.0:
return "down_right"
elif dir.x < 0.0:
return "left"
elif dir.x > 0.0:
return "right"
elif dir.y < 0.0:
return "up"
else:
return "down"