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

1 Like
: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

I am having the same issue.
Character script:

extends CharacterBody3D


@export var speed = 5.0
@export var jump_velocity = 4.5

@onready var neck := $Neck
@onready var camera := $Neck/Camera3D

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	elif event.is_action_pressed("ui_cancel"):
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		if event is InputEventMouseMotion:
			neck.rotate_y(-event.relative.x * 0.0025)
			camera.rotate_x(-event.relative.y * 0.0025)
			camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-30), deg_to_rad(60))

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		velocity.y = jump_velocity

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("Left", "Right", "Up", "Down")
	var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * speed
		velocity.z = direction.z * speed
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		velocity.z = move_toward(velocity.z, 0, speed)

	move_and_slide()

Character Scene:

[gd_scene load_steps=4 format=3 uid="uid://dro20pyrile66"]

[ext_resource type="Script" path="res://Scripts/SceneScripts/Player.gd" id="1_k1o2l"]

[sub_resource type="CapsuleMesh" id="CapsuleMesh_oyyng"]
radius = 0.4
height = 1.75

[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_adynd"]
points = PackedVector3Array(0.392661, 0.519661, -0.0492016, -0.386553, -0.544937, -0.0750427, -0.385615, -0.543624, 0.0803348, -0.337919, 0.681363, -0.049643, 0.0545872, 0.518723, 0.391957, 0.289662, -0.678254, 0.185299, 0.0803347, -0.543623, -0.385615, -0.0750427, 0.544937, -0.386553, -0.175365, -0.458093, 0.359063, -0.302312, 0.508479, 0.257007, 0.10953, 0.85456, 0.0561146, -0.104211, -0.855825, 0.0561936, 0.338863, -0.620311, -0.152356, 0.209532, 0.542855, -0.333283, -0.252356, -0.535709, -0.303308, 0.335215, 0.537002, 0.207303, 0.156568, -0.538457, 0.361722, -0.328435, 0.483949, -0.226512, 0.390359, -0.490698, 0.0801726, 0.0821356, -0.821093, -0.182624, -0.0501032, 0.793014, -0.235148, -0.155459, 0.790984, 0.187271, -0.385615, 0.543623, 0.0803347, -0.0760311, -0.762098, 0.265526, 0.309066, -0.484887, -0.252525, 0.265526, 0.762098, -0.0760311, -0.154309, -0.759156, -0.232813, -0.151909, 0.541332, 0.363639, 0.081474, 0.735424, 0.291639, 0.256255, -0.456308, 0.306965, -0.333283, -0.542855, 0.209532, 0.188524, -0.822964, 0.0558035)

[node name="Character1" type="CharacterBody3D"]
script = ExtResource("1_k1o2l")

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("CapsuleMesh_oyyng")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConvexPolygonShape3D_adynd")

[node name="Neck" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.458436, 0)

[node name="Camera3D" type="Camera3D" parent="Neck"]

Floor Scene

[gd_scene load_steps=3 format=3 uid="uid://duvmujcilw82b"]

[ext_resource type="ArrayMesh" uid="uid://cq165fddd5nys" path="res://Meshes/DungeonFloor2_3x3.res" id="1_2c6p6"]

[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_jllck"]
points = PackedVector3Array(-1.5, -0.05, -1.5, -1.5, 0.05, -1.5, 1.5, -0.05, -1.5, -1.5, -0.05, 1.5, -1.5, 0.05, 1.5, 1.5, 0.05, -1.5, 1.5, -0.05, 1.5, 1.5, 0.05, 1.5)

[node name="DungeonFloor2_3x3" type="StaticBody3D"]

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = ExtResource("1_2c6p6")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConvexPolygonShape3D_jllck")

Replying because I found out what the issue is, or at least a fix:

For static 3d object, your heirarchy MUST be:
MeshInstance3d → StaticBody3d → CollisionShape3d

As in, your mesh must be the parent object, with a single static body 3d child, which has a single collision shape 3d child.

Its not intuitive, you would think the static body should be the parent object. And I don’t even know if I’ve stumbled across the solution or a hack :upside_down_face: