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
in Engine by (27 points)
edited by

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.

by (22,067 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.

by (20 points)
edited by
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.