Godot Version
Godot version4.5.1
Question
its my first try on makeing a game after some learning and i cant seem to work this out. i have set my damage button for now to see how my animation will look. But when i click still "Idle" animation plays. I can also play other animations.
here is the entire code of the character. note that is a mix of chat gpt and youtube help. Thank you
extends CharacterBody2D
class_name Player
@export var HEALTH : float = 100
@export var speed : float = 80.0
@export var sprint_mult : float = 1.5
@export var jump_vel : float = -300.0
@export var gravity : float = 900.0
@export var coyote_sec : float = 0.12
@export var jump_buffer_sec: float = 0.08
@export var accel : float = 0.18
@export var friction : float = 0.18
var _coyote_timer : float = 0.0
var _jump_buffer : float = 0.0
var _facing := 1.0
var can_move := true
@onready var anim : AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) → void:
—-- horizontal + sprint (ONLY place we set velocity.x) ------
var input := Input.get_axis(“move left”, “move right”)
var cur_speed := speed * (sprint_mult if Input.is_action_pressed(“runnig”) else 1.0)
var accel_rate := accel if input != 0.0 else friction
velocity.x = lerp(velocity.x, input * cur_speed, accel_rate)
# —-- gravity --------------------------------------------------
if not is_on_floor():
velocity.y += gravity * delta
_coyote_timer = max(_coyote_timer - delta, 0.0)
else:
_coyote_timer = coyote_sec
# —-- jump / coyote / buffer -----------------------------------
_jump_buffer = max(_jump_buffer - delta, 0.0)
if Input.is_action_just_pressed("move up"):
_jump_buffer = jump_buffer_sec
if _jump_buffer > 0.0 and (_coyote_timer > 0.0 or is_on_floor()):
_jump_buffer = 0.0
_coyote_timer = 0.0
velocity.y = jump_vel
if Input.is_action_just_released("move up") and velocity.y < 0.0:
velocity.y *= 0.5
move_and_slide()
if abs(velocity.x) > 10.0:
_facing = sign(velocity.x)
$AnimatedSprite2D.flip_h = _facing < 0
# ---- animation ----
if Input.is_action_pressed("damage"):
anim.play("hit")
velocity.x = 0.0
if Input.is_action_pressed("attacking"):
anim.play("attack")
velocity.x = 0.0
if Input.is_action_pressed("attacking"):
anim.play("attack")
velocity.x = 0.0
elif not is_on_floor():
anim.play("idle")
elif abs(velocity.x) > 10.0:
anim.play("walking")
else:
anim.play("idle")