# KinematicBody2D collision with tilemap

Attention Topic was automatically imported from the old Question2Answer platform.

I’m creating a top down tile based game. I added tile base movement with the below code:

``````extends character

var initial = Vector2(96,96)
var motion = Vector2()
var target = Vector2(96,96)
var d = 0

export var _gold = 0

func _physics_process(delta):
var _value  = d * speed

if  _value >= 1:
_value = 1
d = 0

self.position.x = lerp(initial.x,target.x,_value)
self.position.y = lerp(initial.y,target.y,_value)

if motion != Vector2():
initial = self.position
target = initial + motion
motion = Vector2()

elif self.position == target:
initial = self.position

if Input.is_action_pressed("ui_up"):
motion = Vector2(0,-1) * TileSize
elif Input.is_action_pressed("ui_down"):
motion = Vector2(0,1) * TileSize
elif Input.is_action_pressed("ui_right"):
motion = Vector2(1,0) * TileSize
elif Input.is_action_pressed("ui_left"):
motion = Vector2(-1,0) * TileSize

else:
d += delta
``````

I also have a tilemap for testing the game. However my character is not colliding with the cells but instead just continues to walk on top of the tile. My character is a KinematicBody2D is there a way to check if it will collide before the movement happens? I tried searching on google for hours but haven’t come up with anything yet.

is there a way to check if it will collide before the movement happens?

For that, you’re best bet is probably a `RayCast2D` (or `RayCast` depending on your needs)…

RayCast2D — Godot Engine (3.2) documentation in English

jgodfrey | 2020-02-17 17:41

I’m not entirely sure how you’re using `_value` and using `lerp` to move but this will cause the movement to possibly glitch through any smaller/thinner colliders like how using `translate` would. I’d recommend using `move_and_slide` or `move_and_collide`. Also the last argument in `move_and_collide` will return any potential collision information like what @jgodfrey was saying.

Magso | 2020-02-17 23:01