0 votes

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.

Godot version 3.2.3.stable.official
in Engine by (108 points)

1 Answer

+1 vote
Best answer

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

by (5,392 points)
selected by

like this?

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

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

this not work right

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.

thank, i put this after match and now all work

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.

Categories