4.6 Updating Animations

This commit is contained in:
siyahas 2024-11-02 01:28:19 +03:00
parent d227c3e098
commit 256d057662

View file

@ -4,6 +4,8 @@ extends CharacterBody2D
@export var speed = 300.0 @export var speed = 300.0
@export var jump_velocity = 400.0 @export var jump_velocity = 400.0
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
# Add the gravity. # Add the gravity.
@ -19,3 +21,21 @@ func _physics_process(delta: float) -> void:
velocity.x = direction * speed velocity.x = direction * speed
move_and_slide() move_and_slide()
play_animations(direction)
func play_animations(direction: float):
if direction != 0.0:
animated_sprite_2d.flip_h = direction == -1.0
if is_on_floor():
if direction == 0.0:
animated_sprite_2d.play("idle")
else:
animated_sprite_2d.play("run")
else:
if velocity.y < 0:
animated_sprite_2d.play("jump")
else:
animated_sprite_2d.play("fall")
pass