Godot Version
godot 4.4.1
Question
so im having trouble with 2d slide animation and am trying to add a spin in between jump and fall animations. Also, i have crouch and slide but crouch lets me still move rather than being static and i try adding is_crouching to slide but it still seems to ignore it and slide whenever.
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
const GRAVITY = 1000
enum State {Idle, Run, Jump, Crouch, Slide}
var current_state
var slide_timer := 0
var slide_timer_max := 1
var slide_duration = 1
var is_sliding
var has_spun = false
var is_crouching = false
func _ready():
current_state = State.Idle
func _physics_process(delta):
player_falling(delta)
player_idle(delta)
player_run(delta)
player_jump(delta)
player_slide(delta)
move_and_slide()
player_animations()
func player_falling(delta):
if !is_on_floor():
velocity.y += GRAVITY * delta
func player_idle(_delta):
if is_on_floor():
current_state = State.Idle
elif is_on_floor() and Input.is_action_pressed(“crouch_press”):
is_crouching = true
current_state = State.Crouch
func player_run(_delta):
var direction = Input.get_axis(“move_left”, “move_right”)
if direction:
velocity.x = direction * 300
else:
velocity.x = move_toward(velocity.x, 0, 300)
if direction != 0:
current_state = State.Run
animated_sprite_2d.flip_h = false if direction > 0 else true
func player_jump(_delta):
if Input.is_action_just_pressed(“jump_press”):
current_state = State.Jump
velocity.y = -400
func player_slide(_delta):
if Input.is_action_just_pressed(“action”):
current_state = State.Slide
velocity.x = 1000
is_sliding = true
slide_timer = slide_duration
func player_animations():
if current_state == State.Idle:
animated_sprite_2d.play(“idle”)
elif current_state == State.Run:
animated_sprite_2d.play(“run”)
if Input.is_action_pressed("crouch_press"):
animated_sprite_2d.play("crouch")
if current_state == State.Slide:
animated_sprite_2d.play("slide")
if velocity.y > 0 :
animated_sprite_2d.play("jump fall")
elif velocity.y < 0 :
animated_sprite_2d.play("jump")
if current_state == State.Jump and velocity.y <= 1 :
has_spun = false
animated_sprite_2d.play("jump spin")
has_spun = true
func _on_jump_spin_finished():
if is_on_floor():
has_spun = false