move process to physics process to avoid issues
This commit is contained in:
parent
1c1be19cfa
commit
6a3b5cb089
1 changed files with 20 additions and 21 deletions
|
|
@ -2,40 +2,39 @@ extends CharacterBody2D
|
||||||
|
|
||||||
const MAX_SPEED: float = 150
|
const MAX_SPEED: float = 150
|
||||||
|
|
||||||
var was_moving: bool
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||||
var is_moving: bool
|
|
||||||
var current_dir: String
|
var was_moving: bool = false
|
||||||
|
var anim_dir: String = 'down'
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
was_moving = true
|
sprite.play('down')
|
||||||
is_moving = false
|
sprite.frame = 1
|
||||||
current_dir = 'down'
|
sprite.pause()
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _physics_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:
|
|
||||||
var movement_vector: Vector2 = get_movement_vector()
|
var movement_vector: Vector2 = get_movement_vector()
|
||||||
var direction: Vector2 = movement_vector.normalized()
|
var direction: Vector2 = movement_vector.normalized()
|
||||||
velocity = direction * MAX_SPEED
|
velocity = direction * MAX_SPEED
|
||||||
|
|
||||||
is_moving = velocity.length() > 10
|
move_and_slide()
|
||||||
|
|
||||||
|
var is_moving: bool = velocity.length() > 10
|
||||||
|
|
||||||
if is_moving:
|
if is_moving:
|
||||||
if abs(velocity.x) > abs(velocity.y):
|
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:
|
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:
|
func get_movement_vector() -> Vector2:
|
||||||
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue