0 votes

Goodnight, I'm learning GDscript and I make a personal challenge to code a double jump for my character, I made it but it feels a little slow sometimes and sometimes doesn´t work, this is the Script I make please tell me if there is something to improve:

<extends KinematicBody2D

const UP = Vector2(0,-1)
const GRAVITY = 20
const SPEED = 200
const JUMP_HEIGHT = -500
const MAX_JUMP_COUNT = 2
var second_jump = false
var jump_count = 0

var motion = Vector2()

func _physics_process(delta):
    motion.y += GRAVITY
    if Input.is_action_pressed("ui_right"):
        motion.x = SPEED
        $AnimatedSprite.play("correr")
        $AnimatedSprite.flip_h = false
    elif Input.is_action_pressed("ui_left"):
        motion.x = -SPEED
        $AnimatedSprite.play("correr")
        $AnimatedSprite.flip_h = true
    else:
        motion.x = 0
        $AnimatedSprite.play("quieto")

    if is_on_floor():
        if Input.is_action_just_pressed("ui_up"):
            motion.y = JUMP_HEIGHT
            jump_count = 1



    motion = move_and_slide(motion, UP)

    if second_jump:
        if Input.is_action_just_pressed("ui_up"):
            motion.y = JUMP_HEIGHT
            jump_count = 2

    if jump_count < MAX_JUMP_COUNT:
        second_jump = true
    else:
        second_jump = false>
asked Sep 20, 2019 in Projects by Lucky_Mouse (14 points)

1 Answer

+1 vote

Nicely done! I think the slow feeling is just the gravity begin to low bumping that up to 100 and the jump to -2000 should fix that.

Then you could add a long jump, mario style, with something like

if Input.is_action_pressed("ui_up") and motion.y<0:
    motion.y += GRAVITY*0.5
else:
    motion.y += GRAVITY 
answered Sep 20, 2019 by flurick (886 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.