Collisions Layers/Masks?

Godot Version

4.2.2

Question

I have a question regarding collision layers and Masks and am hoping someone may be able to clarify if my understanding is wrong.

I never really understood collisions in Unity or Unreal, I just sort of played around with it until it worked. It was never really explained in a simple way. Now I wanted to do something in Godot and sort of maybe got an understanding from one of the replies I got on the question, which maybe wrong!

I sort of got that the layer is the where the object lives like the player lives on “collision layer 2”, never changes. That the hero’s Jam, other things can also live on that layer, but, best just to have one object living there especially seeing as that is pretty important.

The “collision mask” is the layer that the object can interact with. So, like if the tileset is layer 1, we would set the mask to “1” so the “hero” can interact with that layer. So, if an enemy lived on Layer 3, and also needed to interact with the tiles then the mask would be also on 1, and if he can be killed by the ‘hero’ there would also be a mask on 2. Like there could be more masks per character but only one place where they live.

Now is this basically right? Or is there more to it?

Regards.

I think of it like this:

  • Collision layer is what the body lies in. It’s its little world, it just exists in that layer.
  • Collision mask is what the object looks towards when searching for collisions. It’s what it’s interested in.

Some examples:

  • Terrain has collision layer of 1 and player has collision mask of 1. The player is looking for terrain to collide with and when it sees some, it stops.

    • The terrain actually doesn’t need any mask (even though Godot assigns both layer and mask by default), because it doesn’t care if anything collides with it!
  • Hitbox (usually Area2D) could only have player mask assigned, looking hurtbox in player collision layer! When it finds a player in it, it delivers the damage, but the player never even knows what hit him!

    • If you want to detect the hits on player too, you should enable its hurtbox mask to look for the hitbox layer!
  • Let’s say our hurtboxes are looking for what hit them and not the other way around. In this scenario, player hurtbox’s mask is player_damage and enemy’s hurtbox mask is enemy_damage. If you want a spell that damages BOTH player and enemies (like a nuke :radioactive: ), you would set the spell’s layer to BOTH player_damage and enemey_damage. The appropriate hitboxes will then register the collision.

    • You could also model simple invincibility with this system. Just turn off tha player’s *damage collision layers and no spells will be able to hit them!
1 Like