My jump command isn't working for some reason

Godot Version

4.0

Question

Hello!

I’m working on my first real game project with godot here (2D platformer), and things are coming along relatively well, aside from the fact that my jump doesn’t seem to work. When I run it for debugging and do the jump input, it doesn’t change the vertical position. I’ve double and triple checked the input map, and I think it has something to do with it not properly processing the velocity.y in my script, but I don’t really know how to fix it.

Here’s the relevant code for it:

@export var speed = 75
@export var gravity = 400
@export var jump_height = -400

#move states
var attacking = false
var climbing = false

func _physics_process(delta):
#Vertical Velocity(Gravity)
velocity.y = gravity * delta
#horizontal movement processing
horizontal_movement()
#apply movment
move_and_slide()

func horizontal_movement():
#Get Input Strength for Horizontal Movement. Returns -1 for left, 1 for right
var horizontalInput=Input.get_action_strength(“ui_right”)-Input.get_action_strength(“ui_left”)
#calculate horizontal velocity
velocity.x = horizontalInput*speed
if !(attacking||climbing):
player_animation()
move_and_slide()

#animations
func player_animation():
#left run
if Input.is_action_pressed(“ui_left”) || Input.is_action_just_released(“ui_jump”):
$AnimatedSprite2D.flip_h = true
$AnimatedSprite2D.play(“Running”)
$CollisionShape2D.position.x = -7
#right run
if Input.is_action_pressed(“ui_right”) || Input.is_action_just_released(“ui_jump”):
$AnimatedSprite2D.flip_h = false
$AnimatedSprite2D.play(“Running”)
$CollisionShape2D.position.x = -7
#idle
if !Input.is_anything_pressed():
$AnimatedSprite2D.play(“Standing”)
if $AnimatedSprite2D.flip_h:
$CollisionShape2D.position.x = 0
else:
$CollisionShape2D.position.x = -14

#input captures
func _input(event):
#on attack
if event.is_action_pressed(“ui_attack”):
attacking = true
$AnimatedSprite2D.play(“Standing”)
if event.is_action_pressed(“ui_jump”) and is_on_floor():
print(“Trying to jump”) #Debug attempt. It is entering the statement
velocity.y += jump_height
$AnimatedSprite2D.play(“Standing”) #Don’t have a jumpt sprite yet

I know its properly registering the input event, as the print statment goes through, and it resets the flip of the sprite if it isn’t facing the default direction when I hit jump, but nothing I seem to do fixes it.

Thanks in advance for any advice!!

You are applying jump velocity properly when you handle the _input(), but then right before the move_and_slide() in _physics_process() you override it with this line
velocity.y = gravity * delta
So the jump vector is ignored.
You could change this line to
velocity.y += gravity * delta
Whcih should fix the jump, but will change the behavior of your gravity, which might not be desired - depends on your goals.

Figured it would be something minor like that. Thanks!

1 Like