is_on_floor not working
Asked Answered
M

5

0

Hey everyone. I'm new to Godot, so I'm sorry if this is a dumb question. I am making a basic platformer to get used to the engine, as I come from Unity. I created a CharacterBody2D and I tried to give it gravity. I wanted to know if the character is touching the floor so that gravity doesn't apply when this is the case. However, is_on_floor always returns false; even when my character falls into a StaticBody2D.

This is the code I'm using:

extends CharacterBody2D
const SPEED = 300.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta
	move_and_slide()
	print(is_on_floor())

And my scene currently looks like this:

Thank you for your help!

Matthiew answered 20/9, 2023 at 15:38 Comment(0)
I
0

Matthiew Check character's floor settings and make sure that collision layers are properly set up.

Island answered 20/9, 2023 at 15:47 Comment(0)
M
0

Thank you for the fast reply! I have the default values for most properties. All the layers are marked as floor. What else should I be checking?

Matthiew answered 20/9, 2023 at 15:52 Comment(0)
I
0

Matthiew Motion mode must be "Grounded" not "Floating". Also check character's up vector and max wall angle

Island answered 20/9, 2023 at 15:58 Comment(0)
M
0

Island It's funny that I checked all the properties except the most obvious one lol It's working now, thank you again for your help! <3

Matthiew answered 20/9, 2023 at 16:7 Comment(0)
Q
0

check your center of the kinematic body and center of the collision shape is set in the same position. This also happens to me because of the position of kinematic body and collision shape are different.

Quicksand answered 10/2 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.