 This video was brought to you by my patrons. Thank you so much for your support Understanding how collision layers and masks work is essential to make action games in good engine For instance on a platformer game You can implement a jump through mechanic by separating terrains into two layers and toggling the collision off based on players input I have an implementation for that in one of my experience. I'll put the link in the description if you want to check it out You can also prevent enemies from colliding with each other while they still collide with the environment And the player and even combat system rely a lot on the understanding of physics layers For instance a hit in hardbox system often demands an exclusive layer to prevent unwanted Interactions with other systems such as a selection area or site rains for instance I was watching devdux latest devlog and at some point he mentions that he had quite a time managing his games collision layers and masks This is a common issue in Godot. I remember that when I started I had like a lot of issues with that kind of system as well I didn't know how layers was meant to be used and I didn't actually understand what masks are So I had some bad time with that as well To understand this feature better you can think of a physics object from two perspectives From itself why should check to collide with so this will be the masks and from other objects perspective Where they should find this object to collide with it. So the layers For instance, let's make an enemy with a bump behavior. Whenever it collides with something on its sides It inverts its movement direction moving away from whatever it collides with Now if we test the enemy behavior, you can see that it's working as expected It bumps on walls. It changes its movement direction when it collides with the wall But something weird might happen if we add another enemy, so let's close this go to the 2d workspace Duplicate and I'll move it right here. So let's test this out play You can see that now enemies bump on each other as well This can be something that you might want in your game, but usually we don't want that to happen We want enemies to pass through each other and only collide with environment and maybe with the player, right? So I'll close that and how we can achieve that how we can make the enemies don't collide with each other I'll open the enemy scene and here on the physics body category We have the collision and we have the layer and the mask I will remove the enemies from the first layer from the first beat So I'll play the scene again And now you can see that they are avoiding they are passing through each other While they are still colliding with the environment right so with the floor This is because they are still masking the first beat and since the Environment so the floor and the wall are in the first layer. They will collide with it They will detect that there is something that they should collide with in this layer because they are masking the first layer But now there is a problem because let me add another object here the player I will remove the player from the first layer as well So let's play this scene and Now enemies are also ignoring the player they are passing through the player and this is something that shouldn't happen because for instance if we want to implement that Stomping behavior that we have for instance in Mario game Super Mario game Where the player can jump on enemies heads and stomp them We need to know that we collided with something We need the enemies to be in a physical layer and not be just a ghost not be a material entity in the game So for that we need we need to fix that, right? One way we can do that. Let me just remove the player from the Area of the floor One way we can do that is by adding a new layer. So let's go to the enemy here and You can see that we have 20 layers available and 20 masks available to mix and match them to Reach to achieve the behavior that we want so The the way that we can make the enemy interact with the environment not interact with other enemies and allow the player To interact with the enemies is by creating another layer called enemies so that the enemies will be inside this layer instead So the way that we can do that is by going into project project settings And you can see here in layers names. This is not mandatory. Actually, you can just click here and Wherever you will get this behavior, but is a good practice to name your layers So let's name the first layer environment environment and the second layer enemies Right, so let's save that enemies will be in the enemies layer, but they will check for collisions in the environment if we check that Players are still ignoring the the enemies, but enemies are still colliding with the environment so Back in the player scene we can now tell the players to mask the enemies as well So let's save that and test this And now enemies are bumping on players because players are Interacting with the enemies so we can stomp enemies. Let's let's add the stomp behavior So let's see if this works Yep, so there we have it. We have the stomp mechanic Now that we understand how collision layers and collision masks work We can explore their functionalities to create more interesting behaviors We have four methods that allow us to play with collision objects layers and mess The set collision layer bit and its getter counterpart and the set collision mask bit together with its getter counterpart as well These methods allow us to dynamically insert or remove objects into a physics layer or from a physics layer This is exactly how I implement the pastoral jump when the player presses the jump button while holding down The character is top masking the pastoral layer Which allows it to ignore any collisions with platforms and other environment objects that are inside the slayer For more details on that you can read my experiment report the link will be in the description As you can see there are many possibilities to play with once you understand this feature It's especially useful for action games such as platformers as you can see I'm currently with the goal of producing a series of videos that explore all these aspects of a platformer game I just need to reach that two hundred dollars goal on my patronage campaign to start producing this series So consider supporting me there to give me a green light that that's something that you'd like to see here in the channel That's it. Thank you so much for watching keep developing and until the next time