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
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!
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
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
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?
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()
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