How to build a 2d NPC companion that can walk through my Player

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

I’m working on a 2d platformer, and I’m building a companion character to help my Player fight enemies. I want it to be able to walk right through my player, but still collide with things like the floor, walls, enemies, and projectiles.

right now I’ve built my companion as a KinematicBody2d with an AnimatedSprite and rectangle CollisionShape2d. I tried making the CollisionShape2d have a One-Way-Collision, and at least this mostly keeps it from getting in the way of my Player (the Player can just push the companion around), but I want it to be able to pass right through my Player and run directly to the enemy.

How can I accomplish this?

:bust_in_silhouette: Reply From: Eric Ellingson

You want to utilize the collision layer and collision mask.

To make things easier, you’ll probably want to name the layers. See Physics introduction — Godot Engine (3.1) documentation in English for details on setting this up. Given your description, you probably want a layer for: player, npc, floor, walls, enemy, and projectile

Now, for each of these entities, you’ll want to select the “collision layer” that matches what the entity is. So for the player, you want to select the player bit. For floors, you want to select the floor bit.

The “collision mask” indicates which types of things that entity is able to collide with. So for example, for the npc, you would want to have all the things selected except player, this way the npc can pass through the player.