Having an issue with collision layers

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

First question is: Using Gdscript, how do I assign multiple collision layers/masks? Every time i try to set them via script (set_collision_layer(2) and set_collision_layer(3)), it seems to only allows the Area node to have one layer. When I print(collision_layer, collision_mask) it just shows one layer it’s assigned to.

Second, I’m having a weird issue where the Players collision layer and mask is on 1 and the projectile it’s creating has it’s collision layer and mask on 3, but at the moment the projectile spawns, the player’s hit by his own projectile. Why would this be happening when they’re on separate collision masks/layers?

I’m stumped.

:bust_in_silhouette: Reply From: jgodfrey

Regarding the value for set_collision_layer()… Looking at the docs, it’s not very clear (at least, to me), but the value is a single integer value that’s a bitmask. If you hover over the collision layer values in the inspector, it might make more sense.

For example, hovering over the Layer 1 box says Layer 1 | Bit 0 | Value 1 and hovering over Layer 3 says Layer 3 | Bit 2 | Value 4.

To activate only those two layers via a call to set_collision_layer(), you’d specify a value of 5 (the two reported values added together (1 + 4).

So, just add the values for whatever layers you’re trying to activate and pass that into set_collision_layer()

Regarding #2, your collision layer and mask values sound “odd” to me.

The Layer values are the layers the object appears in or is a part of. The Mask values are the layers the object will “scan” for collisions. Typically, I’d expect the object to be on a different layer than it’s scanning for collisions…

That said, it really doesn’t explain the results you describe…

Thank you for the explanation of the values and the way Godot processes them. I definitely didn’t didn’t get that from the documents.

As I have it, Player 1 and Player 2 are on Layer/mask 1 & 2 respectively and I have the projectiles on layer 3 and their respective mask on 3, and 1 or 2 depending on who shoots it, so it can collide with the other player. But they can also collide with other projectiles. But for some reason, the Players are hitting themselves, lol. It’s frustrating and funny at the same time. I’m not sure what’s happening.

Dumuz | 2020-05-15 06:40

Using the logic you explained I figured out why set_collision_layer(3) was hitting the player.
because it was the sum value for enabling layer 1 & 2, thus the projectile was hitting the player who was on layer 1.

Thanks a lot.

Dumuz | 2020-05-15 17:04