How to make a function happen only when a key is held

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

I am trying to make a sprint system for my game but I am unsure how to make my character run only when I am holding down a key because right now the character stays sprinting forever. This is the code I am using:
func _input(event): if(event.is_action_pressed("sprint")): walk_speed = 6.0 print("Sprint Pressed!")

Thanks!

:bust_in_silhouette: Reply From: Andrew Wilkes

This is because the _input function is called once per event. So you press the button and set the character sprinting.

So you need to add an action for stopping the sprinting i.e. a release of the key.

:bust_in_silhouette: Reply From: theMX89

my movement code looks always abit like this:

func _physiks_process(delta):

if Input.is_action_pressed(“move_forward”):
move_and_collide(transform.basis.x * movespeed * delta)
if Input.is_action_pressed(“move_backward”):
move_and_collide(-transform.basis.x * movespeed * delta)

i hope this helps