+1 vote

Hello! The Player Node (KinematicBody2D) has an 8x8 CollisionShape2D, when I try to make it pass through an 8 pixels tall place, it won't fit. When I drop the collision margin to 0.001 it sinks to the ground.
I want it to be able to pass through places exactly it's height and not sink to the ground for obvious reasons. Thanks!

Cannot fit...

Here's the code.

extends KinematicBody2D

 const GRAV = 0.5
 const ACCELERATION = 3.0

 var horizontal_speed = 0.0
 var vertical_speed = 0.0


 func _physics_process(delta):
var velocity = Vector2(horizontal_speed,vertical_speed)
 #Inputs
var input_left = int(Input.is_action_pressed("ui_left"))
var input_right = int(Input.is_action_pressed("ui_right"))
var input_up = int(Input.is_action_pressed("ui_up"))
var input_down = int(Input.is_action_pressed("ui_down"))
var movement_dir = input_right - input_left
 #Horizontal Movement Processing
if abs(horizontal_speed) < 38:
    horizontal_speed += movement_dir * ACCELERATION
if abs(horizontal_speed) > 1:
    horizontal_speed -= horizontal_speed / 32
else:
    horizontal_speed = 0
 #Vertical Movement Processing

move_and_collide(velocity * delta)
velocity = move_and_slide(velocity,Vector2(0,-1))
if is_on_floor():
    vertical_speed = 0
else:
    vertical_speed += GRAV
in Engine by (19 points)

I'm facing this problem too. Did you find any solution?

Sadly, without workarounds, this kind of pixel-perfect collisions are not possible. But the workaround in the tweet could work. Or you could shrink the CollisionShape2D in height, then have an Area2D that checks if the player is in a pixel-perfect gap, and the character is allowed to jump if only the Area2D is not detecting any collisions.

1 Answer

+1 vote

Interesting! You should try to make the CollisionShape2D's height 7 instead of 8.

by (200 points)

Thanks a lot! That is a very simple and easy solution and makes me feel silly to think that I couldn't think of it! But, I kind of want to keep the CollisionShape2D 8 pixels tall... Is there any other way to get around?

Plus, I would be able to jump in a place where my player shouldn't be able to jump if I were to shrink CollisionShape2D, therefore, the sprite would go inside the tile.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.