Is_on_floor always returning false for some reason

Godot 4.2

I’m very new to gdscript in general as well as coding.

So, I tried writing a movement code for a 2d sidescroller platformer, however the “is_on_floor” is returning only false even after the character is on the said floor, which just adds more gravity to the player making it unable to move, since it’s being pushed into the ground consistently every second (it’s every second, right?)

Anyway, I looked it up and many ppl said that you have to define the floor itself, however I really couldn’t figure it out no matter how I tried so I’m asking help from you, good people.

Here’s my code

extends CharacterBody2D

@export var gravity : float = 960.0
@export var speed : float = 300.0


func _process(delta : float) -> void:
	if not is_on_floor():
		velocity.y += gravity * delta

	var direction := Input.get_axis("left","right")
	if direction:
		velocity.x = direction * speed
	else:
		velocity.x = move_toward(velocity.x, 0, speed)

	move_and_slide()

Any help is apreciated! Thank you!

velocity.y should be -= instead of += on the if not is_on_floor: column?

I might’ve read your comment incorrectly, but making it -= instead of += just makes it subtract the gravity every second and the character just flies up endlessly. Also I didn’t really understand the second part of your comment regarding the column, could you elaborate please? Thank you!

oh maybe you are in a 2d node. Did you check the collisions and all of that stuff?

well yeah, it is a 2d node, I forgot to mention that, sry. Every collision shape is on place, I really don’t know what could go wrong.

oh, are the collision shape’s layer and masks alright aswell?

if it isnt anything else it must be the collision layers or masks on the collision dropdown on the nodes

Make sure your CharacterBody2D motion mode is on grounded and the up direction is correct:

Also i recommend you change the _process callback to _physics_process callback, everything physics related should be called inside _physics_process

Thank you so much, I forgot to check that! Also thanks for the advise!

I found the answer, thank you for your help regardless!

yeah np Im just not as familiar on 2d stuff

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.