Why character moves 1 frame

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

I am starting now with Godot, and I don’t understand why if I push the key to move, the character moves just once and then stops (even if key is still pushed)
`extends KinematicBody

var velocity = Vector3 (0,0,0)
const WALK = 5
const RUN = 30
const GRAV = 9.8
const JUMP = 5

Called when the node enters the scene tree for the first time.

func _ready():
pass # Replace with function body.

func handle_input ():
if Input.is_action_just_pressed(“ui_right”) and Input.is_action_just_pressed(“ui_left”):
velocity.x = 0
elif Input.is_action_just_pressed(“ui_right”):
if Input.is_action_just_pressed(“ui_shift”):
velocity.x = RUN
velocity.x = WALK
elif Input.is_action_just_pressed(“ui_left”):
if Input.is_action_just_pressed(“ui_shift”):
velocity.x = -RUN
velocity.x = -WALK
velocity.x = 0
if Input.is_action_just_pressed(“ui_up”) and Input.is_action_just_pressed(“ui_down”):
velocity.z = 0
elif Input.is_action_just_pressed(“ui_up”):
if Input.is_action_just_pressed(“ui_shift”):
velocity.z = -RUN
velocity.z = -WALK
elif Input.is_action_just_pressed(“ui_down”):
if Input.is_action_just_pressed(“ui_shift”):
velocity.z = RUN
velocity.z = WALK
velocity.z = 0
if Input.is_action_just_pressed(“ui_select”) and is_on_floor():
velocity.y = JUMP

func _physics_process(delta):
if not is_on_floor():
velocity.y -= GRAV * delta
move_and_slide(velocity , Vector3.UP)`

Please help!
Thanks a lot in advance :slight_smile:

:bust_in_silhouette: Reply From: jgodfrey

The logic of your posted code is lost since it wasn’t formatted for the forum, so this is just a guess, but…

I assume your problem stems from the fact that you’re using:


… which is only true for the SINGLE frame where an input even is triggered.

Instead, you probably want:


… which is true for EVERY frame where the input is being held.

Yeap, I did not notice it, thanks a lot :smiley: saved my day!

Dimax_93 | 2022-12-13 07:50