This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I am making a metroidvania game and i want to limit how many times the player can jump before colliding. my code is bellow:

extends KinematicBody2D

const GRAVITY = 10 # in pixels
const JUMP = 250 # start speed px/s
const SPEED = 200 # px/s

var velocity = Vector2.ZERO # (0,0)

func _ready():
    pass

func _on_Timer_timeout():
    print(str($Timer.wait_time) + " second(s) finished")

func _physics_process(delta):

    # if no keyboard input for left/right then x speed is 0
    velocity.x = 0 
    if(Input.is_action_pressed("right")):
        velocity.x = SPEED
    elif(Input.is_action_pressed("left")):
        velocity.x = -SPEED

    velocity.y += GRAVITY

    if (Input.is_action_just_pressed("jump")):
        velocity.y -= 250

    velocity = move_and_slide(velocity)
Godot version 3.3.3
in Engine by (37 points)

1 Answer

0 votes

Well, you can have for example

var jumps_available := 2

at the top of your code, then simply

# _physics_process
if Input.is_action_just_pressed("jump") and jumps_available > 0:
    velocity.y -= 250
    jumps_available -= 1

along with some kind of reset condition, such as

# _physics_process
if is_on_floor():
    jumps_available = 2

and that should give a rudimentary kind of double-jump functionality.

by (111 points)

will floor count as any surface?

I don't know, it depends on how you move your character and stuff. If you want to reset on more than just hitting the floor, you can add or change the conditions.

the code doesn't work when on a tilemap? how should i fix this?

I don't know, I don't know what your code looks like at the moment. If your tilemap has colliders, then chances are you aren't moving your character in a way that's compatible with is_on_floor.

do you want me to send my code?

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.