How to fix is_on_floor for KinematicBody 2D

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

So basically every time i move my player the is_on _floor function keeps switching to true and false even though I’m on the floor.

I’m using a tilemap to give collisions to my floor and viewed the collisions shapes while running and there wasn’t any gaps between the tiles.

I’m using Godot 3.1

What your move_and_slide call looks like?

Dlean Jeans | 2019-06-16 06:41

found out my player is bobbling up ad down but very tiny bobs.
don’t know why however.

any ways here’s the script

Player Move script
if Input.is_action_pressed("ui_right"): facing=1 $"Right") motion.x=SPEED elif Input.is_action_pressed("ui_left"): facing=-1 $"Left") motion.x=-SPEED else: motion.x=0

Extends script for player

extends KinematicBody2D

const MAXSPEED=120
const JUMPPOWER=270
const JUMP=Vector2(0,-1)
const GRAV=10

var motion=Vector2()

func move():
	if !is_on_floor():

Newby | 2019-06-16 07:16

:bust_in_silhouette: Reply From: kidscancode

This is not how is_on_floor() works.

The value of is_on_floor() is set by move_and_slide(). Since you’re using the previous frame’s value and then moving with no gravity, the value is being set to false.

You should only check is_on_floor() after calling move_and_slide(). And if you disable gravity, you won’t be moving into the floor, and therefore it there won’t be a collision with the floor.

func move():
    motion.y += GRAV
    motion = move_and_slide(motion, JUMP)
    # now check is_on_floor()

This answer should be posted in the front of docs considering how common this issue is. Well done on explaining it.

Yiannis Charalambous | 2020-08-09 10:52