• 2D
  • is_on_floor() is always False, what mistake am I making?

I have a starter project to learn Godot, it has a whopping 7 lines of code.

extends KinematicBody2D

var gravity = ProjectSettings.get('physics/2d/default_gravity');
var velocity = Vector2.ZERO

func _physics_process(delta):
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity)
	print(is_on_floor()); # always false, but why?

It's my understanding that calling is_on_floor() after move_and_slide() will return True if the body was stopped by a collision below it. Yet it's always false.

In my Scene I have a "Player" with that script attached to it and a "StaticBody2D" which the "Player" falls down onto. Player consists of a KinematicBody2D + Sprite + CollisionShape2D (Rectangle)

You have to pass the floor normal in to set which direction is considered a floor.


const UP = Vector2(0, -1)
move_and_slide(velocity, UP)

Hope that helps.

Thanks, I knew I was doing something dumb! I was following a tutorial which doesn't have that, it must be older than this feature. I need to learn how to read the docs better.

Okay, cool. Glad I could help. I've found the docs to be really robust, but searching for answers on Google can be tough because there are lots of old results for obsolete APIs or wrong information. Luckily the docs are pretty good for Godot.

a year later