Why is_on_floor() always returns false

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

Im want to make a 3D FPS game in Godot 3.4.4 and i have problems with jumping my code ↓↓↓

extends KinematicBody

export var speed = 11
export var accel = 5
export var gravity = 0.98
export var jump_power = 30

onready var head = $Head
onready var camera = $Head/Camera

var velocity = Vector3()

func _physics_process(delta):
	var head_basis = head.get_global_transform().basis

	var direction = Vector3()
	if Input.is_action_pressed("ui_up"):
		direction -= head_basis.z
	elif Input.is_action_pressed("ui_down"):
		direction += head_basis.z

	if Input.is_action_pressed("ui_left"):
		direction -= head_basis.x
	elif Input.is_action_pressed("ui_right"):
		direction += head_basis.x

	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y += jump_power

	direction = direction.normalized()

	velocity = velocity.linear_interpolate(direction * speed, accel * delta)
	velocity.y -= gravity

	velocity = move_and_slide(velocity)

if me printing is on floor()
and im reciving false, why?
P.S.:I always write velocity.y - = gravity because I read other answers and it said: “You always need to move the player a little down”

:bust_in_silhouette: Reply From: TopBat69

I think this might be happening because godot can’t really tell what is a floor or what is a wall or what is a ceiling. To fix this in the move and slide function after velocity add Vector3.Up. It will look like this:

velocity = move_and_slide(velocity, Vector3.UP)

This tells godot that you are above a floor and like that it can detect wall and ceiling
If we did the opposite like Vector3.DOWN Then godot will treat the actual floor as ceiling
And the ceiling as floor

Hope this helps :slight_smile:

Just wanted to add. I believe the colliding bodies are constantly pushing each other away that causes the is_on_floor() function to return false.
That’s why we need a constant force “gravity” to keep them together.
I hope my logic is correct.

shadownight32112 | 2022-09-17 15:44