(gg translate so sorry if it wrong) Jumping problem.

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

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 jump_force = -400
export var extra_jump = 1
var jump_count = 0
func _physics_process(_delta):
motion.y += gravity
if Input.is_action_pressed(“ui_right”):
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)
:bust_in_silhouette: Reply From: p7f

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)

Is still not work, sorry bro.

REd | 2020-08-02 02:27

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

REd | 2020-08-02 02:41

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?

p7f | 2020-08-02 02:48

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.

p7f | 2020-08-02 02:56

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

REd | 2020-08-02 03:09

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”):
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

REd | 2020-08-02 03:17

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)

p7f | 2020-08-02 15:01

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

REd | 2020-08-02 15:16

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.

p7f | 2020-08-02 15:23

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

p7f | 2020-08-02 15:30

Okey, Let me try

REd | 2020-08-02 15:39

Problem sloved, thanks for the help bro.

REd | 2020-08-02 16:21

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

p7f | 2020-08-02 16:34

:bust_in_silhouette: Reply From: REd

extends KinematicBody2D
class_name Strength_demon
const UP = Vector2(0, -1)
var motion = Vector2()
export var speed = 300
export var gravity = 20
export var jump_force = -400
export var extra_jump = 2
var jump_count = 0

#Basic movement
func _physics_process(delta):
motion.y += gravity
if Input.is_action_pressed(“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)