Godot 3.0 KinematicBody2D and StaticBody2D collision not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sumerechny

Hello.

So I followed this tutorial https://www.youtube.com/watch?v=9CKH5fanubM and near the end it explains how to attach collision shapes to objects. I think I did it exactly like in the video, but no matter what I do my character always falls through the floor. This is how my project looks right now problem hosted at ImgBB — ImgBB

Did something change in 3.0? Because I remember it working when I was playing around with Godot 2.1 some time ago.

How are you moving the player? Can you share the player script?

kidscancode | 2018-02-11 22:51

This is the player script

extends KinematicBody2D

var input_direction = 0
var direction = 0

var speed_x = 0
var speed_y = 0
var velocity = Vector2()

const MAX_SPEED = 600
const ACCELERATION = 1000
const DECELERATION = 2000

const JUMP_FORCE = 800
const GRAVITY = 2000


func _ready():
    pass

func _input(event):
    if event.is_action_pressed("ui_spacebar"):
	    speed_y = -JUMP_FORCE


func _process(delta):
    if input_direction:
	    direction = input_direction

if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
	input_direction = 0
elif Input.is_action_pressed("ui_left"):
	input_direction = -1
elif Input.is_action_pressed("ui_right"):
	input_direction = 1
else:
	input_direction = 0

if input_direction == - direction:
	speed_x /= 3
if input_direction:
	speed_x += ACCELERATION * delta
else:
	speed_x -= DECELERATION * delta
speed_x = clamp(speed_x, 0, MAX_SPEED)

speed_y += GRAVITY * delta

velocity.x = speed_x * delta * direction
velocity.y = speed_y * delta
move_local_x(velocity.x)
move_local_y(velocity.y)

pass

Sumerechny | 2018-02-11 22:58

:bust_in_silhouette: Reply From: kidscancode

KinematicBody2D only detects collision when moved using the proper methods, move_and_collide() or move_and_slide(). You will not detect collisions using move_local_x() or other Node2D methods.

Try changing those last 2 lines to move_and_collide(velocity).

Also, as a side note, you should delete pass at the end of the function - it does nothing.

That worked, thanks! :smiley: Seems like I have a lot to learn. It’s a little bit overwhelming.
By the way, can you explain to me why even call func _ready(): when it does nothing? Or at least it seems to me like that in this version of Godot.

Sumerechny | 2018-02-11 23:27

You don’t need _ready() at all if you aren’t putting any code in it. The only reason it’s there is that the default script template puts it there as an example. Whenever you create a new script, you can choose “Empty” instead of “Default” for the template and it won’t insert any code or comments.

kidscancode | 2018-02-11 23:29

Thank you very much!

Sumerechny | 2018-02-11 23:31