This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

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

sean256sean256 Posts: 3Member

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)

Comments

  • cyberealitycybereality Posts: 928Moderator
    edited December 2019

    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.

  • sean256sean256 Posts: 3Member

    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.

  • cyberealitycybereality Posts: 928Moderator

    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.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.