Can I disable collision shapes associated with Area2D in a specific group ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Scavex
if area.is_in_group("example"):
          Disable collision shapes in example

Is something like this possible ?
The thing is that I am trying to make a powerup after taking which the enemies as well as the weapons they fire at me doesn’t affect my player for some time. I have placed the enemies and their fire in a group called damageable. I figured disabling my player’s collision shape for some time will give me same results but then I can’t even take other powerups. So I wanted a way though which my player will be unaffected only by enemies and not other powerups.

If your powerup is about “bullets will go through the player”, my answer is ok.

But if you’re going to make “player is invincible” power-up, you may rather go the way “player health is not reduced when buffed”. This way you may keep the collisions up and use them to ricochet bullets from player or just shatter them into explosion and so on.

Reloecc | 2020-09-14 10:43

:bust_in_silhouette: Reply From: Reloecc

I believe the best way is going via Collision layers / masks

Just change a player’s collision layer for a time the buff (power-up) is up.

I just made collision layers which are Player, Enemies and Powerups. The code I was using earlier was like this

if area.is_in_group("passthrough"):
		$CollisionShape2D.set_deferred("disabled", true) 
		$PassThroughParticles.emitting = true

Now I am assuming I don’t need $CollisionShape2D.set_deferred("disabled", true) part since I wont be disabling my collision shape. But I don’t know how to use collision masks in code. Can you give an example considering I have now three different collision layers ?

Edit : I tried using set_collision_mask_bit(2,4) after seeing the bit and value from the UI but maybe I messed up since after taking the powerup I still collide with enemies. Perhaps I have to do something to make the mask layer where enemies reside false ? If yes, then how can that be achieved ? Thank you very much for your help. BTW 2 and 4 are parameters for Powerup Mask layer.

Edit2: tried using set_collision_mask_bit(1,false). Still colliding with enemies

Scavex | 2020-09-14 11:21

I would

  • put player into collision layers 1 and 2.
  • put bullets into layer 2 only
  • and for active power-up I would player.set_collision_layer_bit(1, false)

Reloecc | 2020-09-14 11:53

Unfortunately these collision layers and masks are not working for me. If I put the Player in Layer 1 and Select Powerup Mask layer and deselect Enemy Layer Mask, the player is still interacting with Enemies.

Scavex | 2020-09-14 12:12

I’ve created this right now as an example for you: Hope it helps. - Google Drive

Reloecc | 2020-09-14 12:31

Oh thank you my friend. Your example helped me. I realized that these masks and layers work two way. I was disabling the Enemy mask for Player but on Enemy layer, Player Mask was still present. I disabled that too and now it’s working. I hope you get all the happiness. Thanks for your precious time :slight_smile:

Scavex | 2020-09-14 12:39