How do i code a sprint?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dandevuni

Its hard to find how to sprint code and i dont know how to do it,not in 3d,but sprinting in 2d,help

maybe increase your move speed, when you click sprinting button?
example :
if event.is_action_pressed("R_click"): move_speed = 200

potatobanana | 2021-03-01 12:59

Did that,but for some reason the animation didnt work

Dandevuni | 2021-03-03 07:09

:bust_in_silhouette: Reply From: bloodsign

if you don’t want to make that big of change to your current code base, assuming you have:

var velocity = Vector2.ZERO
var acceleration = 10

func _physics_process(delta):
 if Input.is_action_pressed("right"):
  velocity.x += acceleration 
 velocity = move_and_slide(velocity)

For your movement code, you could simply add:

var speed_bonus = 0

func _physics_process(delta):
 if Input.is_action_pressed("right"):
  velocity.x += acceleration + speed_bonus

 if Input.is_action_pressed("shift_run"):
  speed_bonus = 10
  speed_bonus = 0
 velocity = move_and_slide(velocity)

Well it worked but,the animation didnt,whenever i tried to move the player,the animation just froze amd doesmt animate,it worked if i use it for anything else but it just doesnt work whenever i tried to use the sprint code

Dandevuni | 2021-03-03 07:08