Player Falling Through Floor

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

So I’m relatively new to Godot, and I just tried making my first 2.5D game. I want to implement gravity, but the “is_on_floor” function is always reporting false, so the player never stops falling. (It just falls through the floor) Both the player and the Floor have collision. I’ve been messing with this for several hours now! Here’s my player script:

extends KinematicBody

var velocity = Vector3(0,0,0)
var SPEED = 1
var accel = 0.1
var gravity = 0.1
var jump = 0

func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity
print(is_on_floor())
if Input.is_key_pressed(KEY_D) and Input.is_key_pressed(KEY_A):
velocity.x = 0
elif Input.is_key_pressed(KEY_D):
velocity.x = min(velocity.x + accel, SPEED)
elif Input.is_key_pressed(KEY_A):
velocity.x = max(velocity.x - accel, -SPEED)
else:
velocity.x = lerp(velocity.x, 0, 0.1)

if Input.is_key_pressed(KEY_W) and Input.is_key_pressed(KEY_S):
	pass
elif Input.is_key_pressed(KEY_S):
	pass
elif Input.is_key_pressed(KEY_W):
	pass
else:
	pass
	#velocity.z = lerp(velocity.z, 0, 0.1)
if not Input.is_key_pressed(KEY_W) and not Input.is_key_pressed(KEY_A) and not Input.is_key_pressed(KEY_S) and not Input.is_key_pressed(KEY_D):
	pass
move_and_slide(velocity)

btw, I haven’t used the jump variable yet. Cant jump it you just fall through the floor!!! If someone can understand what the problem is or needs further info, please reply! Any help is much appreciated! Have a nice day.

-Fired

:bust_in_silhouette: Reply From: juppi

Hey Fired,

you have to specify the up_direction:

move_and_slide(velocity, Vector3.UP)

“up_direction is the up direction, used to determine what is a wall
and what is a floor or a ceiling.”

I put move_and_slide(velocity, Vector3.UP) after the line with “velocity.y -= gravity”, and it didn’t change anything.

FiredReadyBlocks | 2022-12-31 12:54