How do I cap the amount of times a player can jump per second?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PopSquip

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

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

wyattb | 2021-06-08 01:49

:bust_in_silhouette: Reply From: Help me please

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 :slight_smile: