0 votes

Hey, I have this problem with the state of isonfloor on my kinematicBody2D while runing/moving only on the x axis. The state is always switching even though the player is not leaviing the ground at all. Any help would be grat :)

her is my code:

extends KinematicBody2D

export var velocity = Vector2(0,0)
export var slide = 0.2
const SPEED = 280
const JUMPFORCE = -900
const GRAVITY = 30

var flyJump = true


func _physics_process(delta):
    velocity = move_and_slide(velocity, Vector2.UP)
    velocity.x = lerp(velocity.x,0,slide)

    if Input.is_action_pressed("right"):
        velocity.x = SPEED
        $AnimatedSprite.play("run")
        $AnimatedSprite.flip_h = false
    elif Input.is_action_pressed("left"):
        velocity.x = -SPEED
        $AnimatedSprite.play("run")
        $AnimatedSprite.flip_h = true
    else:
        $AnimatedSprite.play("idle")


    if Input.is_action_just_pressed("jump"):
        if flyJump == true:
            velocity.y = JUMPFORCE


    if is_on_floor():
        flyJump = true 

    if !is_on_floor():
        coyoteTime()
        velocity.y += GRAVITY
        print("xxx")
        $AnimatedSprite.play("jump")

func coyoteTime():
    yield(get_tree().create_timer(1), "timeout")
    flyJump = false
asked Jun 22 in Engine by BigBackPack (23 points)
edited Jun 22 by kidscancode

2 Answers

+2 votes

In order for is_on_floor() to work properly, you must apply gravity at all times, not just when in the air.

answered Jun 22 by kidscancode (16,966 points)
0 votes

You might want to try move_and_slide_with_snap() instead of move_and_slide(). Your issue might be caused by bumps on the ground (there are even tiny gaps between tiles if you are using a tile map). move_and_slide_with_snap() will make sure your kinematic body sticks to the ground.

Note: you have to disabled snap when you want to jump.

answered Jun 22 by qez008 (20 points)
edited Jun 22 by qez008
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.