second – Pixel platformer character collides with blocks above whereas there’s nonetheless a visual hole

second – Pixel platformer character collides with blocks above whereas there’s nonetheless a visual hole

[ad_1]

I used to be making an attempt to the arrange a tile map for a 2D pixel artwork sport in Godot 4.1 (following a tutorial) and realized that there appears to be a problem or one thing I’m not conscious of with the way in which collisions work.

Every time my character jumps within the -y course up in the direction of the block, the collision seems to occur a bit to early (see photos), which results in a really unintuitive expertise. However this solely occurs when approaching the block from -y course, from some other course (with various velocity) the character will get as shut as to the block as meant by the collision’s form.

The identical happens when involving the block as CollisionShape2D, as an alternative of a tile map.

What am I lacking right here?

Character CollisionShape2D

Character not colliding appropriate

Character colliding as intended

Right here is the character script:

extends CharacterBody2D

const SPEED = 150.0
const JUMP_VELOCITY = -300.0
const ACCELERATION = 0.25

# This may change, on account of the truth that it will depend on 
#the bottom floor or debufs utilized to the character
const AIR_FRICTION = 0.05
const GROUND_FRICTION = 0.6

@onready var animatedSprite2D = $AnimatedSprite2D

# Get the gravity from the venture settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/second/default_gravity")

func _physics_process(delta):
    
    move_and_slide()
    
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta
        velocity.x = lerp(velocity.x, 0.0, AIR_FRICTION)

    # Deal with Bounce.
    if Enter.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the enter course and deal with the motion/deceleration.
    var course = Enter.get_axis("ui_left", "ui_right")
    
    if course:
        set_facing_direction(course)
        velocity.x = lerp(velocity.x, course * SPEED, ACCELERATION)
        animatedSprite2D.play("Run")
    else:
        velocity.x = lerp(velocity.x, 0.0, GROUND_FRICTION)
        animatedSprite2D.play("Idle")

func set_facing_direction(course):
    if(course > 0):
        animatedSprite2D.flip_h = true
    if(course < 0):
        animatedSprite2D.flip_h = false

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply