0 votes

I'm thinking the way to jump twice and jump height at the same time, works fine but for some reason I can again jump endlessly. Is there a viable solution?

const UP = Vector2(0, -1)
var motion = Vector2()
export var speed = 300
export var gravity = 20
export var jumpforce = -400
export var extra
jump = 1
var jumpcount = 0
func _physics
process(delta):
motion.y += gravity
if Input.is
actionpressed("uiright"):
motion.x = speed

``````elif Input.is_action_pressed("ui_left"):
motion.x = -speed

else:
motion.x = 0

if is_on_floor():
jump_count = 0
if Input.is_action_just_pressed("ui_up"):
motion.y = jump_force
elif (jump_count == 1):
if Input.is_action_just_pressed("ui_up"):
motion.y = jump_force
jump_count = 0
if Input.is_action_just_released("ui_up") and jump_count == 0:
motion.y = 0
motion = move_and_slide(motion, UP)
``````
in Engine

2 Answers

–1 vote
Best answer

extends KinematicBody2D
classname Strengthdemon
const UP = Vector2(0, -1)
var motion = Vector2()
export var speed = 300
export var gravity = 20
export var jumpforce = -400
export var extra
jump = 2
var jump_count = 0

Basic movement

func physicsprocess(delta):
motion.y += gravity
if Input.isactionpressed("ui_right"):
motion.x = speed

``````elif Input.is_action_pressed("ui_left"):
motion.x = -speed

else:
motion.x = 0
``````

Jump

``````if is_on_floor():
jump_count = 0
if Input.is_action_just_pressed("ui_up") and jump_count <= extra_jump:
motion.y = jump_force
jump_count += 1
if Input.is_action_just_released("ui_up") and jump_count == 0:
motion.y = 0
motion = move_and_slide(motion, UP)
``````
by (17 points)
+1 vote

you should check if cont == 0 in the first if where you check if `ui_up` is pressed... otherwise you will end up entering that if always you press that button. Also, if after the second jump, you set jump_count to 0 aggain, you will be able to keep jumping.. Try something like

``````if is_on_floor():
jump_count = 0
if  jump_count == 0 and Input.is_action_just_pressed("ui_up"):
motion.y = jump_force
jump_count = 1
elif (jump_count == 1):
if Input.is_action_just_pressed("ui_up"):
motion.y = jump_force
jump_count = 2
if Input.is_action_just_released("ui_up") and motion.y < 0:
motion.y = 0
motion.y += 980*delta
motion = move_and_slide(motion, Vector2.UP)
``````
by (3,505 points)

Is still not work, sorry bro.

And the point is double jump, player fall down when i release up key, and I can change the jumping time at extra_jump

I understood that the point is double jump, but the way you coded it, you could jump always, as you are not checking if you already jumped once, so you always enter the first if. I modified a bit the code.. would you try it? or would you share a minimal project reproducing your issue so i can test here?

I just made a simple scene, i added the code i edited in my answer, and tested. double jump is working for me with that exact code in `_physics_process`. Note i also added gravity, because i didnt know how you handled it on your code.

``````if is_on_floor():
if Input.is_action_just_pressed("ui_up"):
motion.y = jump_force
if Input.is_action_just_released("ui_up") and motion.y < 0:
motion.y = 0
motion = move_and_slide(motion, UP)
``````

So this is the code that jump one and jump interrupt work well, now just need a duoble jump

if isonfloor():
jumpcount = 0
if Input.is
actionjustpressed("uiup") and jumpcount <= extrajump:
motion.y = jump
force
jumpcount += 1
if Input.is
actionjustreleased("uiup"):
motion.y = max(motion.y, 0)
motion = move
and_slide(motion, Vector2.UP)

This code work kind of well, but always jump three times even change the extra_jump

But have you tried the code i provided? because it double jumps and interrupts correctly in my machine.. here i provide another code for any value of extra jump that works on my pc fine:

``````extends KinematicBody2D

var velocity = Vector2(250,0)
const SPEED = 550
var jump_count = 0
var motion : Vector2 = Vector2.ZERO
var jump_force = -600
var extra_jump = 5

func _ready():
pass

func getinput():
if Input.is_action_pressed("ui_right"):
velocity.x = SPEED
elif Input.is_action_pressed("ui_left"):
velocity.x = -SPEED
else:
velocity.x = 0

func _physics_process(delta):

if is_on_floor():
jump_count = 0
if  jump_count < extra_jump and Input.is_action_just_pressed("ui_up"):
motion.y += jump_force
jump_count += 1
if Input.is_action_just_released("ui_up") and motion.y < 0:
motion.y = 0
motion.y += 980*delta
motion = move_and_slide(motion, Vector2.UP)
``````

Yeah I tried, and your new code, do you sure it is work? Because It just dont even move

Yes, im sure.. im trying it right now.. Of course, it wont move because i just coded the jump, as that was what was aksed in the question.

Also, check that ive added gravity and jump force myself because it was not provided. Maybe youll need to tweak those values.

Okey, Let me try

Problem sloved, thanks for the help bro.

no problem! if the answer helped, you may select it so others see its solved.