is_on_floor() switches between true and false

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

When sitting on the tiles, my character’s is_on_floor() switches between true and false every frame. Does anyone know what is causing this and how to fix it? If you need my code just tell me and I can give it to you. Thanks

Can you give the code?

exuin | 2021-06-15 03:17

Most common reason why this happen is because of floor itself. Your floor is not flatten enough that your kinematic body treat it as air etc. Try resetting the rotation of your floor and go back here

Mrpaolosarino | 2021-06-15 03:55

   extends KinematicBody2D

onready var jump_buffer_timer : Timer = $JumpBufferTimer
onready var anim : AnimationPlayer = $AnimationPlayer
onready var sprite : Sprite = $Sprite
onready var coyote_timer : Timer = $CoyoteTimer

const ACCELERATION : int = 512
const MAX_SPEED : int = 58
const FRICTION : float = 0.25
const AIR_RESISTANCE : float = 0.2
const GRAVITY : int = 350
const JUMP_FORCE : int = 100

var motion : Vector2 = Vector2.ZERO

var jump_buffer : bool = false
var hit_ground : bool = false
var coyote_time : bool = false

func _physics_process(delta : float) -> void:
	var x_input : float = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	if x_input != 0:
		anim.play("run")
		motion.x += x_input * ACCELERATION * delta
		motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
	else:
		anim.play("idle")
	motion.y += GRAVITY * delta
	if is_on_floor():
		coyote_time = true
		if x_input == 0:
			motion.x = lerp(motion.x, 0, FRICTION)
		if jump_buffer == true:
			motion.y = -JUMP_FORCE
		if hit_ground == false:
			hit_ground = true
			sprite.scale = Vector2(1.6, 0.4)
	else:
		hit_ground = false
		if motion.y > 0:
			anim.play("fall")
		else:
			anim.play("jump")
		if x_input == 0:
			motion.x = lerp(motion.x, 0, AIR_RESISTANCE)
		if coyote_timer.is_stopped() == true:
			coyote_timer.start()
	if Input.is_action_just_pressed("ui_jump"):
		if coyote_time == true:
			motion.y = -JUMP_FORCE
		else:
			jump_buffer = true
			jump_buffer_timer.start()
	if Input.is_action_just_released("ui_jump") && motion.y < 0:
		motion.y *= 0.6
	sprite.scale = lerp(sprite.scale, Vector2(1, 1), 0.2)
	motion = move_and_slide(motion, Vector2.UP)
	
func _on_JumpBufferTimer_timeout():
	jump_buffer = false

func _on_CoyoteTimer_timeout():
	coyote_time = false

djmick | 2021-06-15 13:04

I check the floor and everything is working there it seems.

djmick | 2021-06-15 13:05