Hello everyone,
sadly i really struggled with adding a smooth jump to my character. I took some code of an tutorial and put it in my game (but just a jump part). But its not working right. The tutorial code just works awesome so i copied the jump code from it but in my code the character just gets teleported to a higher point and i dont see the difference.
This is the full Tutorial script:
extends KinematicBody2D
const GRAVITY = 600
const WALK_SPEED = 200
const JUMP_FORCE = 500
var velocity = Vector2()
var screen_size
func _ready():
screen_size = get_viewport_rect().size
func _physics_process(delta):
velocity.y += delta * GRAVITY
if Input.is_action_pressed("ui_left"):
velocity.x = -WALK_SPEED
elif Input.is_action_pressed("ui_right"):
velocity.x = WALK_SPEED
else:
velocity.x = lerp(velocity.x, 0, 0.1)
if Input.is_action_pressed("ui_up") and is_on_floor():
velocity.y = -JUMP_FORCE
velocity = move_and_slide(velocity, Vector2.UP)
and this is my code:
extends KinematicBody2D
export var jump_force = 0.0
var jump_velocity = Vector2()
var gravity = 100
var speed = 300
var jump_speed = 200
const GRAVITY = 600
const WALK_SPEED = 200
const JUMP_FORCE = 1500
func _ready():
pass
func _physics_process(delta):
var velocity = Vector2()
velocity.y += delta * GRAVITY
var move = Vector2()
if Input.is_action_pressed("ui_left"):
move.x = -speed
$AnimatedSprite.flip_h = false
if Input.is_action_pressed("ui_right"):
move.x = speed
$AnimatedSprite.flip_h = true
if Input.is_action_pressed("ui_up") and is_on_floor():
velocity.y = -JUMP_FORCE
move_and_slide(move,Vector2(0, -1))
velocity = move_and_slide(velocity, Vector2.UP)