How to get wall-collision working in top-down 2d game?

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

Hi all,

I am trying to make a topdown 2d game in which the player collides with walls. At first I worked with a regular area2D, but according to an answer to this question asked before: https://forum.godotengine.org/16587/how-do-i-create-collision-with-walls, it was easier to use a kinematicbody2d and a staticbody2d to minimize code. I then followed this article: Kinematic character (2D) — Godot Engine (latest) documentation in English to try to make my collision work. This however doesn’t work, as my player can still move through my intended walls.
I tried to use this code for my player:

extends KinematicBody2D

const SPEED = 400

func _physics_process(delta):
    var velocity = Vector2()
    ...
    # Movement input
    ...
    if velocity.length() > 0:
        velocity = velocity.normalized() * delta * SPEED
    if move_and_collide(velocity):
        print("Collide!")

to test whether the collision is actually working, but this prints nothing. For both the player and my tiles the collision layers and masks are set to the top left box.

Is there anyone who knows what I’m doing wrong?

Thanks in advance! :slight_smile:

~FortunePilot

:bust_in_silhouette: Reply From: deaton64

Hi,

I think you’ve got your collision layers wrong.
Set your player layer to the first box and it’s mask to the second box.
Set the tile layer to the second box and it’s mask to the first box.

Collision layers and masks

Tried this, still doesn’t work. I have left trying to use move_and_collide or move_and_slide, so I’ll be working on my own collision system. Thanks for the help anyways!

FortunePilot | 2020-04-23 10:33