You are browsing as a guest. Sign up (or log in) to start making projects!

Open comments for this post

52m 22s logged

Sprinting!

took a bit, but I finally got sprinting to work.
Works pretty simply, the characters motion is determined by a speed variable, which just increases if the sprint button (shift) is held down.
Here’s the code:
if Input.is_action_just_pressed(“sprint”) and stamina >= 1:
state_machine.travel(“sprint”)
sprinting_timer.start()
SPEED = 200
sprinting = true
elif Input.is_action_just_released(“sprint”):
SPEED = 75
sprinting = false
sprinting_timer.stop()
I’m using an animation_tree for my animations, instead of just a player, this helps the transitions from animations feel smoother, so thats what the “state_machine” is. The “sprinting” variable is used to force other animations (e.g, for idling and running) to check if the player is sprinting, and thus not override the animation (the sprinting animation currently is identical to running, but when I start designing the character i’ll change that). The timer is just on a .5 sec loop, and makes the stamina decrease when it ends, the second bit of code checks if you stopped sprinting, and returns the SPEED to normal and stops the timer loop.

This code doesn’t actually fully work, as another if statement is needed. The extra “and” in the first check makes sure you can’t start sprinting if stamina is below one, but actually only checks once when you start sprinting, not constantly, so the sprinting doesn’t stop if stamina goes below 1 (like it should).
if stamina < 1:
SPEED = 75
sprinting_timer.stop()
sprinting = false

This code, unlike the first “if”, checks consistantly if stamina’s below one, and does the same job as if you let go of sprinting. This keeps stamina from going negative D:

In the future I plan on adding a gradient to the effect, so the speed up isn’t as jarring, but this is a good start!

Thanks to Brackeys for the current assets! (go watch his video on Godot, it’s very informative)

~wil

0
2

Comments 0

No comments yet. Be the first!