Fix the animations in this code: extends CharacterBody2D
@export var speed: float = 300.0
@export var sprint_speed: float = 600.0
@export var acceleration: float = 15.0
@export var friction: float = 10.0
@export var rotation_speed: float = 10.0
@export var movement_threshold: float = 10.0
@onready var body_sprite = Body@onreadyvarlegssprite=Legs
enum AnimationState { IDLE, WALKING, RUNNING, STRAFING_LEFT, STRAFING_RIGHT, ACTION }
var current_state: AnimationState = AnimationState.IDLE
var current_action: String = ""
func _ready() -> void:
body_sprite.animation_finished.connect(_on_animation_finished)
legs_sprite.animation_finished.connect(_on_legs_animation_finished)
_play_idle_animations()
func _physics_process(delta: float) -> void:
var input_direction = Vector2(
Input.get_axis("A", "D"),
Input.get_axis("W", "S")
).normalized()
_handle_movement(input_direction, delta)
_handle_rotation(delta)
_handle_action_inputs()
move_and_slide()
func _handle_movement(input_direction: Vector2, delta: float) -> void:
var target_speed = _get_current_speed()
# Move character based on the input direction
if input_direction != Vector2.ZERO:
velocity = velocity.lerp(input_direction * target_speed, acceleration * delta)
if current_state != AnimationState.ACTION:
_update_movement_animations(input_direction, target_speed)
else:
velocity = velocity.lerp(Vector2.ZERO, friction * delta)
if velocity.length() < movement_threshold and current_state != AnimationState.ACTION:
_transition_to_state(AnimationState.IDLE)
func _handle_rotation(delta: float) -> void:
var mouse_pos = get_global_mouse_position()
var target_angle = (mouse_pos - global_position).angle()
rotation = lerp_angle(rotation, target_angle, rotation_speed * delta)
func _handle_action_inputs() -> void:
if current_state != AnimationState.ACTION:
if Input.is_action_just_pressed("melee"):
_play_action_animation("Melee")
elif Input.is_action_just_pressed("reload"):
_play_action_animation("Reload")
func _get_current_speed() -> float:
return sprint_speed if Input.is_action_pressed("sprint") else speed
func _update_movement_animations(input_direction: Vector2, current_speed: float) -> void:
if current_state == AnimationState.ACTION:
return
var local_velocity = velocity.rotated(-rotation)
# Debugging output to verify animation logic
print("Velocity: ", velocity, ", Local velocity: ", local_velocity)
print("Input direction: ", input_direction, ", Current state: ", current_state)
# Prioritize strafe for horizontal movement
if abs(local_velocity.x) > abs(local_velocity.y): # Horizontal movement (strafe)
if local_velocity.x < 0:
_transition_to_state(AnimationState.STRAFING_LEFT)
else:
_transition_to_state(AnimationState.STRAFING_RIGHT)
# If moving vertically, switch to walking/running
elif abs(local_velocity.y) > abs(local_velocity.x): # Vertical movement (forward/backward)
if current_speed == sprint_speed:
_transition_to_state(AnimationState.RUNNING)
else:
_transition_to_state(AnimationState.WALKING)
func _transition_to_state(new_state: AnimationState) -> void:
if current_state == new_state:
return
print("Transitioning from ", current_state, " to ", new_state)
current_state = new_state
match new_state:
AnimationState.IDLE:
_play_idle_animations()
AnimationState.WALKING:
_play_walking_animations()
AnimationState.RUNNING:
_play_running_animations()
AnimationState.STRAFING_LEFT:
_play_strafe_animations("Strafe_Left")
AnimationState.STRAFING_RIGHT:
_play_strafe_animations("Strafe_Right")
func _play_idle_animations() -> void:
print("Playing Idle animations")
body_sprite.play("Idle")
legs_sprite.play("Idle")
legs_sprite.speed_scale = 1.0
func _play_walking_animations() -> void:
print("Playing Walk animations")
body_sprite.play("Walk")
legs_sprite.play("Walk")
legs_sprite.speed_scale = 1.0
func _play_running_animations() -> void:
print("Playing Run animations")
body_sprite.play("Run")
legs_sprite.play("Run")
legs_sprite.speed_scale = 1.5
func _play_strafe_animations(strafe_direction: String) -> void:
print("Playing Strafe animations: ", strafe_direction)
body_sprite.play("Walk") # Body remains in "Walk"
legs_sprite.play(strafe_direction) # Legs switch to strafe
legs_sprite.speed_scale = 1.0
func _play_action_animation(action_name: String) -> void:
print("Playing Action animation: ", action_name)
current_action = action_name
_transition_to_state(AnimationState.ACTION)
body_sprite.play(action_name)
legs_sprite.stop()
func _on_animation_finished() -> void:
if current_state == AnimationState.ACTION:
current_action = ""
if velocity.length() > movement_threshold:
_update_movement_animations(velocity.normalized(), velocity.length())
else:
_transition_to_state(AnimationState.IDLE)
func _on_legs_animation_finished() -> void:
if current_state != AnimationState.ACTION:
if velocity.length() > movement_threshold:
_update_movement_animations(velocity.normalized(), velocity.length())
else:
_transition_to_state(AnimationState.IDLE)