How the Player recognizes collisions

Godot Version

v4.4

Question

I have an issue with my CharacterBody2D in Godot 4. 
My player gets stuck in the  platform (StaticBody2D) after spawning. 
I can move left and right, but is_on_floor() does not return true, and jumping isn't possible with my code.


Why does is_on_floor() not always detect the ground correctly?
How can I prevent the player from getting stuck inside the platform?

What works so far:

  1. Gravity is working correctly; the player falls onto the platform.

  2. Collision detection seems to work because move_and_collide() detects the StaticBody2D.

However, is_on_floor() is unreliable, and my character stays stuck inside the platform.

In Game:

Code:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0


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("ui_accept") 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 direction := Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)

    move_and_slide()

So the player spawns inside the platform? Or they spawn above the platform and fall onto it?

Yes when i start the game the player drops on to the platform and gets stuck in it.

Did you change any settings from the characterbody node? Like safe margin or something like

start by using a capsule shape instead of a box. a box has corners and corners can get stuck.

yes. any property changed will have a symbol next to it. clicking it will restore the value to the default.

also, make sure there is no scaling applied to a physics body or colliders. there should be a bright yellow alert next to the node in the tree.
to change a collider size set a different set of dimensions, do not use scale. this applies to any parent of the collider or physics body.

Ok thanks for the help i will try it out.

I dont know why its still nor working

I’ve realized that when i changed the shape of the static body form a worldboundry to a rectangle i dont get stuck in the static body. But i still cant jump.

Oh it was a worldboundary, that makes sense, those shapes won’t allow anything to be on a side as indicated by the big red arrow.

I’m not sure about the jumping though. In the code you posted, the line after the if statement checking for jumping is not indented correctly. You should indent it one more line.

it still wont work. I dont know man. I even tried it with prints

Its working it was because slide on ceiling was turned on by default. I turned it of and now it works. And thank y’all so much for the help. :grin:

2 Likes