Make a character jump.

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

I’m making a 2d left-to-right style game (In the same vain as games like hollow night, but with a very different story and looks) and I have the following code inside the func get_input(). I have other code in their (more inputs) so if that is necessary please let me know. This code doesn’t work. Pressing up appears to do nothing, although “Worked” is printed.

if Input.is_action_just_pressed("up"):
           velocity.y -= 20000
       print("Worked")

This is also in the script.

func _physics_process(delta):
    get_input()
    velocity.y += 100
    velocity = move_and_slide(velocity)

Have you tried using is_action_pressed()?

Ertain | 2022-06-19 18:51

:bust_in_silhouette: Reply From: Tentamens

Hi there!
typically when you are making input you don’t put them in an Input func but just slap them in _physics_process so the way I would do it would to
(in _physics_process)

	
   gravity += 30
   if Input.is_action_just_pressed("up"):   
      direction.y = -700
   direction.y = gravity

direction = move_and_slide(direction, Vector2.UP)

hope this help!
if it doesn’t work feel free to respond to the comment with questions!

:bust_in_silhouette: Reply From: Pomelo

I havent worked with platformers for a while, and I dont know the rest of your code, so take it with a grain of salt, but I think I know what is your problem.

Every frame your velocity.y value goes up by 100, and never resets, so in very little time it would have a value way above the 20000. So by the time you try to substract that amount the value remains positive, and the player dosnt jump. You can see if this is the case by doing a print(velocity.y) in the procces(). A quick fix would be to do velocity.y = 20000 intead of velocity.y += 20000, becasue this way you dont care what value velocity.y had before. There are other ways, like having gravity not working when on floor and what not, but this would do.