extends CharacterBody2D
@export var jump_count : int = 0
var current_jump_count : int
@export var SPEED = 150.0
@export var JUMP_VELOCITY = -200.0
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed(“spacebar”) and is_on_floor():
velocity.y = JUMP_VELOCITY
current_jump_count += 1
var collision = move_and_collide(velocity * delta)
if collision and current_jump_count >= 1 :
velocity = velocity.bounce(collision.get_normal())
velocity.y *= 2.2
jump_count -= 1
if velocity.y < -10:
print_debug("I am jumping! Velocity = " + str(velocity.y))
if collision and current_jump_count >= 1 :
current_jump_count -= 1
jump_count -= 1
if current_jump_count and jump_count >= 1:
current_jump_count -= 1
jump_count -= 1
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
I have multiples questions if its not too much of a hassle:
- when jumping from a higher height the player jumps DUPER high, how can i cap the height of the player?
- Sometimes when jumping from a higher platform the player does not jump but instead keeps the jump and uses it when landing on the higher surface again when landing, how do i stop this?