From 256d057662234e0bcee3e9b1a3714eefcdac8428 Mon Sep 17 00:00:00 2001 From: siyahas Date: Sat, 2 Nov 2024 01:28:19 +0300 Subject: [PATCH] 4.6 Updating Animations --- scripts/player.gd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/player.gd b/scripts/player.gd index 570800f..92fc186 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -4,6 +4,8 @@ extends CharacterBody2D @export var speed = 300.0 @export var jump_velocity = 400.0 +@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D + func _physics_process(delta: float) -> void: # Add the gravity. @@ -19,3 +21,21 @@ func _physics_process(delta: float) -> void: velocity.x = direction * speed 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