Issue with instantaneous jump

Godot Version

<4.2>

Question

I’m making a 2d platformer type game and I’ve managed to make the following code and for the most part it works as intended but I keep having an issue where the jump is instantaneous when I’d like there to be a more gradual acceleration to max height. I’m new to coding but I’ve spent a long time trying to get it to work the way I desired and every time I try a solution it either fails to resolve the issue or causes other issues elsewhere. I’ve attached my script and my node tree is just a CharicterBody2d with both an AnimatedSprite and CollisonShape as children.

extends CharacterBody2D

@onready var ani = $AnimatedSprite2D

var movement = Vector2()
var speed = 100
var jump_ht = -10000
var fall_vel = 5
var curr_direc = "Idle"


func _ready():
	
	pass
	
	
func _physics_process(delta):
	
	
	curr_grav()
	plr_movemnt()
	move_and_slide()	
	
	movement = movement.normalized() * speed * delta
	
	
	
	
func plr_movemnt():
	
	var LEFT =Input.is_action_pressed("Left")
	var RIGHT =Input.is_action_pressed("Right")
	var JUMP =Input.is_action_just_pressed("Jump")
	
	
	movement.x = -int(LEFT) + int(RIGHT) 
	movement.y = -int(JUMP)
	
	if movement.x != 0:
		velocity.x = movement.x * speed
	else:
		velocity.x = 0
	
	if is_on_floor() and JUMP:
		ani.play("up")
		velocity.y += jump_ht
	
	
func curr_grav():
	var new_grav = grav_force.new()
	
	velocity.y = fall_vel
	
	if !is_on_floor():
		fall_vel += new_grav.grav_str
	
	if is_on_floor():
		fall_vel = 5
	
	if fall_vel >= new_grav.term_vel:
		fall_vel = new_grav.term_vel

	
# Animation controller
func _process(_delta): ...
	

Thank you for any advice you can offer

That’s just a matter of how you apply the jump force. Here you are doing it all at once in one frame. You could set a frame counter to apply it gradually and stop if the jump button is released before the counter finishes. That way you let the player adjust the jump height by releasing early, or get the maximum height by holding. After the counter has ran out you just apply the normal free falling process until the character is grounded (or some other condition gives you a bonus jump). It is tricky to set up because it’s not something you actively think about when playing a game, but you could slow down the game (or use the debug stepper) to see the effects more clearly.

I’ve tried that a few different ways and couldn’t seem to get any of them to work properly, I get what the problem is I am just unsure how to fix it in Godot.
I figured the movement.y = -int(JUMP) would have handled the jump in the first place but I couldn’t seem to get that to work either, every tutorial I’ve seen just seems to have the jump naturally work, sorry I’m new to Godot and this seems like an easy fix but I am unsure how.

Tutorials seem to have a way to miss important parts of programming. You are converting booleans to integers for your movement, then using that to decide the velocity of the character. That is all convoluted and counterintuitive. You may be better off finding a different tutorial, even in the Godot manual that’s linked in this forum (Documentation). Or maybe just a more general tutorial on how programming works. I am not sure what your intent is, but that tutorial seems it’s prevening you from acheiving it, rather than helping.

1 Like

Ok thank you, it wasn’t just one tutorial I was looking at several, trying to compare how different people coded similar things and none seemed to have the issue where the jump force was applied all at once. I just ended up scraping my curr_grav() function, I probably just need to learn how to use Godot’s classes system better. I adding the following script to my _physiscs_process() function which solved my issue.

func _physics_process(delta):

	if !is_on_floor():
		velocity.y += fall_vel
	if velocity.y > 1000:
		velocity.y = 1000
1 Like