- Edited
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)