Godot Version
4.3
Question
How would I play the hurt animation once and then switch back to the idle animation? Right now the game starts in the hurt animation an only plays the idle animation after the player takes damage.
extends CharacterBody2D
var decelerate_on_jump_realease = 0.5
var walk_speed = 150.0
var run_speed = 200.0
var acceleration = 0.1
var jump_force = -400.0
var deceleration = 0.1
var health = 4
var hit_by_enemy = false
var time_in_seconds = 0.5
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _on_ready():
if hit_by_enemy == false:
$AnimatedSprite2D.play(“idle”)
func _physics_process(delta):
# Gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Jumping.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_force
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y *= decelerate_on_jump_realease
#Running.
var speed
if Input.is_action_pressed("run"):
speed = run_speed
else:
speed = walk_speed
#Left/Right movement.
var direction := Input.get_axis(“left”, “right”)
if direction:
velocity.x = move_toward(velocity.x, direction * speed, walk_speed * acceleration)
else:
velocity.x = move_toward(velocity.x, 0, walk_speed * deceleration)
move_and_slide()
func hit(posx):
hit_by_enemy = true
if hit_by_enemy == true:
$AnimatedSprite2D.play(“hurt”)
velocity.y = jump_force * 0.7
if position.x < posx:
velocity.x = 200
elif position.x > posx:
velocity.x = -200
$AudioStreamPlayer2D.play()
if hit_by_enemy == true and health>3:
health -= 1
$HUD/health_full.set_self_modulate(Color(0,0,0,0))
hit_by_enemy = false
elif hit_by_enemy == true and health>2:
health -= 1
$HUD/health_three_fourth.set_self_modulate(Color(0,0,0,0))
hit_by_enemy = false
elif hit_by_enemy == true and health>1:
health -= 1
$HUD/health_half.set_self_modulate(Color(0,0,0,0))
hit_by_enemy = false
elif hit_by_enemy == true and health>0:
health -= 1
$HUD/health_quarter.set_self_modulate(Color(0,0,0,0))
hit_by_enemy = false
if health == 0:
await get_tree().create_timer(time_in_seconds).timeout
get_tree().change_scene_to_file(“res://scenes/death_screen.tscn”)
elif hit_by_enemy == false:
$AnimatedSprite2D.play(“idle”)
Input.action_release(“left”)
Input.action_release(“right”)