check tile with collision on side

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

I created a tileset from titles with and without collisions, my player is a KinematicBody2D, he moves changing the position, that is, in jerks, I want to determine when moving to one of the four sides if there is a title with collisions in that place or it is not there.

:bust_in_silhouette: Reply From: exuin

You can use a RayCast2D in order to check if there’s a collision shape where the character will move next.

like this?

func checkCollision(side: String) -> bool:
    match side:
	    	$RayCast2D.cast_to = Vector2(0, -16)
    		$RayCast2D.cast_to = Vector2(-16, 0)
    		$RayCast2D.cast_to = Vector2(0, 16)
	    	$RayCast2D.cast_to = Vector2(16, 0)

	if $RayCast2D.get_collider():
    	return true
    	return false

this not work right

Timofey | 2021-02-28 16:16

RayCast2D calculates intersection every physics frame (see Node), and the result is cached so it can be used later until the next frame. If multiple queries are required between physics frames (or during the same frame) use force_raycast_update after adjusting the raycast.

exuin | 2021-02-28 17:35

thank, i put this after match and now all work

Timofey | 2021-02-28 18:49