0 votes

I want to make a cap for the amount of time the player can jump per second how should i go about doing this?

extends KinematicBody2D

const GRAVITY = 10 # in pixels const
JUMP = 25 # start speed px/s
const SPEED = 480 # px/s var
velocity = Vector2.ZERO # (0,0)
func ready():
pass func _on
Timertimeout():
print(str($Timer.wait
time) + " second(s) finished")
func physicsprocess(delta): # if no keyboard input for left/right then x speed is 0 velocity.x = 0
if(Input.isactionpressed("right")): velocity.x = SPEED

elif(Input.isactionpressed("left")):

velocity.x = -SPEED velocity.y += GRAVITY if (Input.isactionjust_pressed("jump")):

velocity.y -= 250 velocity = moveandslide(velocity)

Godot version 3.3
in Engine by (37 points)

Your sample code is hard to read because you did not format it properly.

1 Answer

+1 vote

Godot keeps track of the amount of time passed since the engine started; this time can be used as a reference for planning future actions. So we can use OS.get_ticks_msec() for capping jumps per second. So just add these variables and codes to your script

var jump_cooldown_time = 1000
var next_jump_time = 0

Script:

if event.is_action_pressed("jump"):
  # Check if player can jump
  var now = OS.get_ticks_msec()
  if now >= next_jump_time:
    velocity.y -= 250
    #add cooldown time for jump
    next_jump_time = now + jump_cooldown_time

Hope it will help :)

by (940 points)
edited by
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.