Character going in ground, depth depending on speed of gravity

Godot Version

4.4

Question

So I’m making a character controller in Godot, using my own gravity system, and whenever it lands on a surface, it kind of goes in it a little, depending on the speed of how fast it’s falling. I’ve tried what I can, so decided to come here for advice.

It’s an animated sprite with 3 hitboxes in it, one left, one bottom, and one right for colliding with the floors and walls.

I’ve checked, and no, the collision shapes are not a scale different than 1, 1. So that can’t be the issue.

Here’s the code:

extends AnimatedSprite2D

var currentGravity = 0;

var gravityForce = 10;

var speed = 200;

var collidingBottom = false;

var collidingLeft = false;
var collidingRight = false;

var jumping = false;

func _process(delta: float) -> void:
    if !collidingBottom || jumping:
        currentGravity += gravityForce*delta;

        self.position.y += currentGravity;
    
    if Input.is_action_pressed("Left") && !collidingLeft:
        self.position.x -= speed*delta;
    if Input.is_action_pressed("Right") && !collidingRight:
        self.position.x += speed*delta;
        
    if Input.is_action_just_pressed("Jump") && collidingBottom:
        jumping = true;
        
        currentGravity = -3;

func _on_collisions_area_entered(area: Area2D) -> void:
    if area.name == "Bottom Hitbox":
        collidingBottom = true
    
        jumping = false;
    
        currentGravity = 0;
    if area.name == "Right Hitbox":
        collidingRight = true;
    if area.name == "Left Hitbox":
        collidingLeft = true;
    
func _on_collisions_area_exited(area: Area2D) -> void:
    if area.name == "Bottom Hitbox":
        collidingBottom = false;
    if area.name == "Right Hitbox":
        collidingRight = false;
    if area.name == "Left Hitbox":
        collidingLeft = false;

Why not use a CharacterBody2D with your own gravity?

I didn’t know much about them and like doing my own stuff more, but I’ll use one if it’s the better option.

I think it’s a good idea to learn about how Godot handles collisions with bodies, not only areas.

Most CharacterBody2D script will use move_and_slide() to apply a velocity property to the character’s position and slide along any other colliding bodies, like StaticBody or RigidBody. You could use move_and_collide(velocity * delta) to handle collisions yourself.

Thank you, I’ll read up on this