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:
Gravity is working correctly; the player falls onto the platform.
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.
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()
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.
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.