All Questions


Oliver Sainsbury

The Jump is very small and quick

I have check the Player's varibles (speed.x = 800, speed.y = 1400, gravity = 4000) and they are set the same as in the video. However, when I jump I only get the smallest of hops before coming back down very quickly.

Code below.

extends KinematicBody2D
class_name Actor

const FLOOR_NORMAL: = Vector2.UP

export var speed: = Vector2(300.0, 1000.0)
export var gravity: = 4000.0

var _velocity: = Vector2.ZERO

extends Actor

func _physics_process(delta: float) -> void:
	var is_jump_interrupted: = Input.is_action_just_released("jump") and _velocity.y < 0.0
	var direction: = get_direction()
	_velocity = calculate_move_velocity(_velocity, direction, speed, is_jump_interrupted)
	_velocity = move_and_slide(_velocity, FLOOR_NORMAL)

func get_direction() -> Vector2:
	return Vector2(
		Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
		-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0

func calculate_move_velocity(
		linear_velocity: Vector2,
		direction: Vector2,
		speed: Vector2,
		is_jump_interrupted: bool
	) -> Vector2:
	var out: = linear_velocity
	out.x = speed.x * direction.x
	out.y += gravity + get_physics_process_delta_time()
	if direction.y == -1.0:
			out.y = speed.y * direction.y
	if is_jump_interrupted:
		out.y = 0.0
	return out

That I have to go back and edit to comment to add markup is a bit silly.

  • Nathan Lovato replied

    This line adds delta time to gravity instead of multiplying it:

    out.y += gravity + get_physics_process_delta_time()

    It should be

    out.y += gravity * get_physics_process_delta_time()

    So you end up with maybe 60 times more gravity than expected, pulling the character down to the ground almost instantly.

  • S
    Oliver Sainsbury replied

    Thank you for the quick reply. I spend a lot of time looking it over and completely missed it.

  • N
    Marco Nila replied

    This exact thing happened to me too XD

    Thanks for the answer.