is_on_floor() is not working well

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

the is_on_floor() is not working properley.
when I try to jump :

	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_force

is not working but this will be work? :

	if Input.is_action_just_pressed("jump"):
		velocity.y = jump_force


The code:

extends KinematicBody2D
export var gravity = 20;
export var speed = 10;
export var jump_force = -1000;
var velocity = Vector2(0,0)
func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		velocity.x = speed * 100
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -speed * 20
	velocity.y = velocity.y + gravity
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_force
	velocity = move_and_slide(velocity)
	velocity.x = lerp(velocity.x,0,0.2)


Please post your code properly, it is impossible to read like this.

When posting your question, there is a “Code Sample” button, it looks like {}.

kidscancode | 2021-01-27 19:05

:bust_in_silhouette: Reply From: stianskjelbred

I have the exact same issue. Following for updates!!!

Did you have any luck with:

if Input.is_action_just_pressed("jump"): velocity.y = jump_force


Stian Skjelbred,

:bust_in_silhouette: Reply From: whiteshampoo

You need to specify the up_direction in move_and_slide, so it knows where the floor is.


velocity = move_and_slide(velocity, Vector2.UP)
:bust_in_silhouette: Reply From: rockgg

if anyone still having these problems, make sure you set your collision layers properly and not assign different bodies with the same layers. for example: assign anything thats ground as Layer 1 and the player body as Layer 2. And only assign the Layer 1 to interact with Layer 2 and vice versa.

Edit: it has produce problems on my end in the long run. not recommended