is_on_floor problem

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

Using Godot 3.0.4 I have a StaticBody2D with a CollisionShape2D as the ground. I have a KinematicBody2D also with a CollisionShape2D. Attached to the KinematicBody2D I have the following script:

extends KinematicBody2D

func _physics_process(delta):
var moveBy = Vector2(100, 0)
self.move_and_slide(moveBy, Vector2(0, -1))

	print("On floor")
	move_and_collide(Vector2(0, 5))

The Output only constantly shows “Falling” though which tells me that is_on_floor() isn’t ever set to true. I’ve tried changing the physics engine from the new to the old type and nothing changes. Shouldn’t this code work and when the kinematic body collides with the static body it should start printing out “On floor” or am I missing something?

:bust_in_silhouette: Reply From: rustyStriker

In order for is_on_floor to detect the floor collision the kinematic_body must keep falling, in order to fix your problem you can just add a small value to the initial move_and_slide

and another thing, the is_on_floor and other functions derived from move_and_slide work only after you use the move_and_slide for that specific frame(for example if you will replace the move_and_collide with move_and_slide and then reuse the is_on_floor it will work properly)