My KinematicBody2D freezes in mid air for a half second then continues to fall when falling

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

Hi there, here is my jump function.
I’m having a very strange issue where if my character falls from a very tall height, he will fall, but stop in mid air, then continue to fall. I’m using move_and_slide in my physics process function if that helps.
Oddly enough, I have a friction function that doesn’t seem to apply friction either. However I’ve disabled it to make sure that wasn’t the cause, and it wasn’t.
PS. I know I have two is_on_floor checks, I’ll fix that later, but at the moment, that isn’t the issue either. I’ve disabled the second one and I still had the issue.
I thought it might be the delta but I’m not entirely sure how to use it.

Many thanks!

func jump(delta):
if is_dead == false: 
	if is_on_floor():
		jumps_left = 2
	elif next_to_wall():
		jumps_left = 1
		
	if Input.is_action_just_pressed("up") and jumps_left > 0:
		#if I'm falling - ignore fall velocity
		if velocity.y > 0: 
			velocity.y = 0
		velocity.y = JUMP_POWER
		jumps_left -= 1
		#wall jump
		if not is_on_floor() and next_to_left_wall():
			velocity.x += JUMP_POWER
		if not is_on_floor() and next_to_right_wall():
			velocity.x -= JUMP_POWER
	
	#if still jumping, start to fall
	if Input.is_action_just_released("up") and velocity.y < 0:
		velocity.y *= FALL_SPEED
	on_ground = false
	
	if is_on_floor():
		if on_ground == false:
			is_attacking = false
		on_ground = true
	else:
		if is_attacking == false:
			on_ground = false
			$Sprite.play("spinning")
else:
	velocity = Vector2()

Edit: Hi guys, I’ve done some further testing and have found that when it reaches a specific number (1953 or 1955), for some reason it resets to 1 immediately after, which is causing me to suddenly stop in mid air for a second.

EDIT: FOUND THE ISSUE!!!
I have a gravity function (that I copy and pasted from a tutorial) that clamps the falling speed if it hits 2000. Ha…

func gravity():
if not dashing:
	velocity.y += GRAVITY

if velocity.y > 2000:

velocity.y = 0 #clamp falling speed

if next_to_wall() and velocity.y > MAX_WALL_SLIDE_SPEED and (Input.is_action_pressed("right") or Input.is_action_pressed("left")): 
	velocity.y = MAX_WALL_SLIDE_SPEED #wall slide
:bust_in_silhouette: Reply From: djmick

There is no need to set velocity to 0 before changing it to jump power, because if it was at 60, then you change it to 0, then to -100 for example, that is no different that just changing it directly. I’m not sure, but that could be part of your problem. I hope this helps, but I’m not sure.

Thanks for the reply!
I commented it out and unfortunately still have the same issue.

I suspected it has to do with velocity randomly being reset, so I put a print statement to print out the velocity values.

For some reason, whenever it hits 1953 or 1955, it resets to 1, which is why I kinda freeze in mid-air, because it’s recalculating falling.

bluenaxela | 2020-11-07 19:24

:bust_in_silhouette: Reply From: bluenaxela

I have a gravity function (that I copy and pasted from a tutorial) that clamps the falling speed if it hits 2000. Ha…

func gravity():
if not dashing:
velocity.y += GRAVITY
if velocity.y > 2000:
velocity.y = 0 #clamp falling speed
if next_to_wall() and velocity.y > MAX_WALL_SLIDE_SPEED and (Input.is_action_pressed(“right”) or Input.is_action_pressed(“left”)):
velocity.y = MAX_WALL_SLIDE_SPEED #wall slide