How to make my speed and animation play when the timer finishes?

Godot 4.3

Hello! I’m trying to make a test where when the player starts running, his speed is very slow, and the timer starts. After the timer finishes, the Player speeds up and the animation changes, but I can’t figure out how, can someone help me?

IMPORTANT CODE:

func _ready():
animated_sprite.play(“Idle”)
SPEED = 100

(This code below is under physics process):
if animated_sprite.animation == “Idle”:
$Timer.stop()
SPEED = 100

var direction = Input.get_axis(“left”, “right”)
if direction:
velocity.x = direction * SPEED
$Timer.start()
animated_sprite.play(“Run (SLOW)”)
else:
animated_sprite.play(“Idle”)
$Timer.stop()
velocity.x = move_toward(velocity.x, 0, 6)

(This is not under physics process):
func _on_timer_timeout():
SPEED = 350
animated_sprite.play(“Run (FAST)”)

First thing, is SPEED a const or var? You have it written as a const, but you are changing its value like a var. You don’t want to use all caps generally for variables.

Also, this isn’t necessary, but I’d make a basic statemachine rather than checking which animation is playing.

enum State{IDLE,SLOW,FAST}

var curr_state : State = State.IDLE

You would then change the curr_state var whenever you change animation to idle or moving respectively. Then you could use

if curr_state == State.IDLE:

const SPEED = 100

enum State{IDLE,SLOW,FAST}

var curr_state : State = State.IDLE

func _ready():
     animated_sprite.play(“Idle”)

func _physics_process(delta):
     var direction = Input.get_axis(“left”, “right”)
     if direction:
          if curr_state == State.IDLE:
               curr_state = State.SLOW
               velocity.x = direction * SPEED
               $Timer.start()
               animated_sprite.play(“Run (SLOW)”)
          elif curr_state == State.SLOW:
               velocity.x = direction * SPEED
          else:
               velocity.x = direction * (SPEED * 3.5)
               animated_sprite.play(“Run (FAST)”)
     elif not curr_state == State.IDLE:
          animated_sprite.play(“Idle”)
          curr_state = State.IDLE
          $Timer.stop()
          velocity.x = move_toward(velocity.x, 0, 6)
     move_and_slide()

func _on_timer_timeout():
     curr_state = State.FAST

This won’t reset the timer if you change directions however.

Wasn’t able to test it (on phone), but this is at least a launching pad for you.

2 Likes

Well when I use the enum and var below:

enum State{IDLE,SLOW,FAST}

var curr_state : State = IDLE

it gives me an error saying:

“Identifier “IDLE” not declared in the current scope”

How do I fix this?

Do this instead:
var curr_state : State = State.IDLE

2 Likes

thx a lot

2 Likes

Yeah sorry that was my bad, I wrote it I’m pretty sure in all the others but forgot to put it there haha.

Does this code do roughly what you want?

Yes thank you very much