move process to physics process to avoid issues

This commit is contained in:
yuki 2025-11-13 02:59:29 -03:00
parent 1c1be19cfa
commit 6a3b5cb089

View file

@ -2,40 +2,39 @@ extends CharacterBody2D
const MAX_SPEED: float = 150
var was_moving: bool
var is_moving: bool
var current_dir: String
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
var was_moving: bool = false
var anim_dir: String = 'down'
func _ready() -> void:
was_moving = true
is_moving = false
current_dir = 'down'
sprite.play('down')
sprite.frame = 1
sprite.pause()
func _process(delta: float) -> void:
if not is_moving and was_moving:
$AnimatedSprite2D.play('down')
$AnimatedSprite2D.frame = 1
$AnimatedSprite2D.pause()
if is_moving:
$AnimatedSprite2D.play(current_dir)
func _physics_process(delta: float) -> void:
func _physics_process(_delta: float) -> void:
var movement_vector: Vector2 = get_movement_vector()
var direction: Vector2 = movement_vector.normalized()
velocity = direction * MAX_SPEED
is_moving = velocity.length() > 10
move_and_slide()
var is_moving: bool = velocity.length() > 10
if is_moving:
if abs(velocity.x) > abs(velocity.y):
current_dir = 'right' if velocity.x > 0 else 'left'
anim_dir = 'right' if velocity.x > 0 else 'left'
else:
current_dir = 'down' if velocity.y > 0 else 'up'
anim_dir = 'down' if velocity.y > 0 else 'up'
was_moving = true
sprite.play(anim_dir)
else:
if was_moving:
sprite.play('down')
sprite.frame = 1
sprite.pause()
move_and_slide()
was_moving = is_moving
func get_movement_vector() -> Vector2:
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')