Player hovering above ground collisionshape2d issue (?)

I have an issue where my player just hovers slightly above ground which seems to mess with the reactions with enemies and the players movement logic.

a few others on reddit and I have looked through the code and can’t find any issues. I think it has something to do with collisionshape2d settings but no matter what I do I can’t seem to find a solution.

I’ve googled a bit but can’t seem to find a solution

image

Please ask if you need more information (code, settings, etc)
any and all help is appreciated and in advance thank you

My first guess that your collision shape is slightly bigger than your player sprite or the tilemap collision shape is slightly smaller than the tile.

Can I see your player sprite and collision shape and the tile map collision shape in the editor?

Sorry for late reply, was eating dinner with family

image

Rectangle is collisionshape while capsule is knockback collisionshape
(red is attack area)

But they should all be aligned which is why I don’t understand the problem

That is very weird. Unfortunately, I don’t see anything wrong with the setup and I can’t think of any other reason the character should float.

A bandaid fix would be moving your sprite down to match the height.

Have you looked at or played the game with collision shapes drawn? Might help show you what’s going on.

If it’s not the character, it may be the tilemap. Does your tilemap have space above the top of the grass? Maybe the collision boundary on the tile is set above where the actual image ends.

Yeah i thought the same but i think the hovering is the source of some other issues that happen when the player reacts with walls and enemies. I also checked the tilemap but it doesn’t seem to stick out

When playing with collisionshapes drawn the collisionshape2d for the player also hovers above ground which makes me suspect the problem is elsewhere but I have no idea where to even start looking

Hmm, I still think this is most likely related to collision. I don’t know tile maps too well but, I think since your player looks good, it’s probably the floor tile. Check the collision on the floor tile and adjust where it’s collision box is. You might find it’s filling the whole box, if it is, maybe trim the top off.

Unless you did something super funky with your code and added say, position.y -10 somewhere in your movement I don’t see that being the issue.

Regardless, you got this, let us know when you figure it out!

Just found this which shows you how to modify collision polygons on tilemaps.

After some testing i added a rigidbody2d structure and made it fall to the ground, the problem seems to lie in the player code somewhere since the collisionshape2d is aligned to floor, I’m gonna try to go through all my player code manually and see if I can find the problem :sob:

Will update in case I find anything

Would gravity cause it to fall and collide with the floor on the next frame (if it worked)?

Maybe the code is detecting a collision on the next frame and cancelling movement altogether, rather than closing the gap (which is less than the full amount it would want due to the ground).

What happens if you change the gravity constant? Does it end up closer/farther from the ground?

hmmm, i don’t so but i’m still relatively new to godot so i might just not be understanding the question.

extends CharacterBody2D
#-------------------------------------------
var SPEED = 300.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var latest_direction = 1
#-------------------------------------------
@onready var facing = null
@onready var animation_tree: AnimationTree = $AnimationTree
@onready var state_machine: character_state_machine = $Character_state_machine
@export var normaltimescale : float = 1.0
@export var slowmotimescale : float = 0.5
#-------------------------------------------
func _ready():
	animation_tree.active = true
	endslowmo()
#-------------------------------------------
func _physics_process(delta):
	velocity.x = move_toward(velocity.x, 0, SPEED)
	if not is_on_floor() :
		velocity.y += gravity * delta

I started this project with a tutorial that said to write this so I don’t know how I would go about changing gravity.

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

I have already tried applying gravity constantly to the player but that didn’t change anything

But still I don’t think so since the object fell and stayed on the ground but that might have something to do with the way rigidbody2d structures’ gravity behaves and possibly differs from the way I apply gravity to the player

image

I think you might be onto something i disabled my current gravity mechanics and implemented something else and changed the gravity as high as I could and it seemed to work when standing still although it reverted back to the hover position when the player starts running

Do you know a solution if the code is detecting a collision on the next frame and cancelling movement altogether?

Sorry I’m not too good at explaining. Still kinda new to Godot too and I’m more used to topdown physics…

But the Godot docs have a sample platformer project you might find informative.

https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/character_body_2d_starter.zip

Here’s its player script. Tested it and it seems to be closing the gap with the platforms correctly.

extends CharacterBody2D

var speed = 300.0
var jump_speed = -400.0

# Get the gravity from the project settings so you can sync with rigid body nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	# Add the gravity.
	velocity.y += gravity * delta

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

	# Get the input direction.
	var direction = Input.get_axis("ui_left", "ui_right")
	velocity.x = direction * speed

	move_and_slide()

damn, i’m using the exact same for gravity :sob:

gonna look through the doc tomorrow
I really appreciate the help

To anybody who finds this after the fact. The issue was in the collision settings in the level where I instantiated the player. There is a safe margin value that raised the player 5 pixels above the ground which just needed to be turned down

3 Likes

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