Godot Version
Godot 4.5.1 stable
Question
Why is my animation constantly starting instead of playing?
extends CharacterBody2D
var speed = 50.0;
var jump_vel = -170.0;
var crouching = false;
@onready var anims = $animations;
func _physics_process(delta: float) -> void:
#gravity
if not is_on_floor():
velocity += get_gravity() * delta;
#jump.
if Input.is_action_pressed("jump") and is_on_floor():
velocity.y = jump_vel;
#walk
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * speed;
else:
velocity.x = move_toward(velocity.x, 0, speed);
if Input.is_action_pressed("down"):
crouching = true;
speed = 30;
jump_vel = -70;
else:
crouching = false;
speed = 50;
jump_vel = -170;
#animate
if velocity.x > 0:
anims.play("walk");
$Sprite2D.flip_h = false;
elif velocity.x < 0:
anims.play("walk");
$Sprite2D.flip_h = true;
if velocity.x == 0:
anims.play("idle");
if crouching:
if velocity.x > 0:
anims.play("crouch_walk");
$Sprite2D.flip_h = false;
elif velocity.x < 0:
anims.play("crouch_walk");
$Sprite2D.flip_h = true;
if velocity.x == 0:
anims.play("crouch_stand");
if not is_on_floor() and not crouching:
anims.play("fall");
move_and_slide()
