I wanted to make a 2D platformer and couldn't make the jumping work. I used the same method to add jumping to my other games, but somehow this time it doesn't work, The player teleports in the air. Can anyone help me to make it work?

I am using Godot 3.2.3 and opengles 2.0

Here is the code I'm using:

extends KinematicBody2D

var speed = 230
export var gravity = 300
export var maxJumps = 2
var jumps
var jumpSpeed = 5000

var velocity = Vector2()

func _ready():
	jumps = maxJumps

func _physics_process(delta):
	velocity = Vector2.ZERO
	if fly_enabled == false:
		velocity.y += gravity
	
	
	if Input.is_action_pressed("Left"):
		velocity.x = -speed
		$AnimatedSprite.play("Walking")
		$AnimatedSprite.flip_h = true
		$AnimatedSprite.speed_scale = 1
	elif Input.is_action_pressed("Right"):
		velocity.x = speed
		$AnimatedSprite.play("Walking")
		$AnimatedSprite.flip_h = false
		$AnimatedSprite.speed_scale = 1
	else:
		$AnimatedSprite.play("Idle")
		$AnimatedSprite.speed_scale = 0.25
	if Input.is_action_pressed("Left") and Input.is_action_pressed("Right"):
		velocity.x = 0
	
	
	if fly_enabled:
		if Input.is_action_pressed("Jump"):
			velocity.y -= speed
		if Input.is_action_pressed("Groundpound"):
			velocity.y += speed
	else:
		if is_on_floor():
			velocity.y = 0
			jumps = maxJumps
		if is_on_wall():
			jumps = maxJumps
		if jumps != 0:
			if Input.is_action_just_pressed("Jump"):
				velocity.y = -jumpSpeed  #* delta * 8000 / 4
				jumps -= 1
	
	velocity = velocity * delta * speed
	move_and_slide(velocity, Vector2.UP)

5000 seems like a large number. 5000 * 0.15

speed is 230 jump is 5000

flying: velocity.y += speed jumping: velocity.y = -jumpSpeed

velocity = velocity delta speed?

@dotted said: 5000 seems like a large number. 5000 * 0.15

speed is 230 jump is 5000

flying: velocity.y += speed jumping: velocity.y = -jumpSpeed

velocity = velocity delta speed?

Thanks. But with 5000 * 0.15 as the jumpspeed the player barely even comes off the ground. And when i posted this, i deleted some of the variables from the code, But i forgot to delete the flying part.

I fixed it. I played around with the gravity and jumping values and i finally made it work. Thanks.

This was your velocity calculation in line 53.

velocity = velocity delta speed

so not only are you adding speed or jumpSpeed in velocity, but then these values are multiplied by speed and delta

So its 5000 230 delta, or 34 x 5000, not 750.

2 years later