Why isnt my player colliding with my tilemap?

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

I created a tilemap. For one of my single tiles I put collision inside of it. Then I made a player which is a kinematic body and has Collision Shape 2D inside of it. The player has a script inside of it with the following code:

func _process(delta):
if not moving:
elif dir != Vector2():
	moving = false

func move(delta):
counter += delta * speed
if counter >= 1.0:
position = initpos + dir * tileSize
counter = 0.0
moving = false
position = initpos + dir * tileSize * counter

func set_dir(): #Set moving
dir = get_dir()
if dir.x != 0 or dir.y != 0:
moving = true
initpos = position

func get_dir(): #Get User Input
var x = 0
var y = 0
if dir.y == 0:
x = int(Input.is_action_pressed(“ui_right”)) - int(Input.is_action_pressed(“ui_left”))
if dir.x == 0:
y = int(Input.is_action_pressed(“ui_down”)) - int(Input.is_action_pressed(“ui_up”))
return Vector2(x, y)

The player isnt colliding with the tilemap but instead goes right through it. What did I do wrong?

:bust_in_silhouette: Reply From: kidscancode

You’re not using the kinematic body correctly. If you just change its position directly, you will not get collisions. You must use the KinematicBody2D movement methods, move_and_collide() or move_and_slide().

This is explained in the docs: Using KinematicBody2D