Godot Version
``extends CharacterBody2D
var speed = 300.0
const JUMP_VELOCITY = -400.0
const RUN_SPEED = 350.0
const SLIDE_SPEED = 700.0
var sliding = false
var sliding_cooldown = false
@onready var health = 100
@onready var playersprite = $playersprite
@onready var playeranimation = $playersprite/playeranimation
func _process(delta: float) → void:
if velocity.x == 0:
print(velocity.x)
anim_handler()
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("space") and is_on_floor():
velocity.y = JUMP_VELOCITY
var direction := Input.get_axis("left", "right")
if direction and sliding == false:
velocity.x = direction * speed
else:
velocity.x = lerp(velocity.x, direction * SLIDE_SPEED, delta * 5.0)
if Input.is_action_just_pressed("slide") and sliding_cooldown == false:
playeranimation.play("idle")
speed = SLIDE_SPEED
$Timer.start()
anim_handler()
move_and_slide()
func anim_handler():
if velocity.x > 0:
playeranimation.play(“player_walking”)
$playersprite.scale.x = 7
$playercollision.scale.x = 1
elif velocity.x < 0:
playeranimation.play(“player_walking”)
$playersprite.scale.x = -7
$playercollision.scale.x = -1
if velocity.x == 0:
playeranimation.stop()
playeranimation.play(“idle”)
func _on_timer_timeout() → void:
sliding_cooldown = true
playeranimation.play(“idle”)
speed = RUN_SPEED
$cooldown.start()
func _on_cooldown_timeout() → void:
sliding_cooldown = false``
Question
I was thinking how should I make the animations so the code would be cleanier to read and also how to stop walking animation and start idle animation when player doesnt move