0 votes

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(collisionlayer, collisionmask) 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.

in Engine by (341 points)

1 Answer

+1 vote
Best answer

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...

by (10,916 points)
selected by

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.

Using the logic you explained I figured out why setcollisionlayer(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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.