How to add function that will move "up" constant and smoothly while key is held

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

Hi All,
I Am fairly new to game development and coding, so apologies if this is a newbie question/problem

How to add function in Godot that will move “up” constant and smoothly while key is held and then when key released for gravity to work

The idea is to have movement for the “player” similar to, if a balloon was filled with air it would rise and then if released it would fall, the code below is what i have tried , it works kind of , although it is still more of a jump action as i need to keep pressing, rather than holding for a smooth elevation. I have also tried “is_action_pressed” also but as I hold it freezes the sprite until I release then it performs the jump

extends KinematicBody2D

const GRAVITY = 10
var motion = Vector2(80,0)

func _physics_process(delta):
apply_gravity()

func apply_gravity():
if Input.is_action_just_pressed(“move up”):
motion.y = -500
else:
motion.y += GRAVITY
move_and_slide(motion)

:bust_in_silhouette: Reply From: Lopy

Input.is_action_pressed(“move up”) should work. Maybe try Input.get_action_strength(“move up”) instead?

When something behaves unexpectedly, the general solution is to add prints to pinpoint where things went weird. You could try a print(motion_y) at the end, as well as a print(“Pressed”) inside the first case of your if.