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

in Engine by (403 points)

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.

1 Answer

0 votes
Best answer

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

https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html?#collision-layers-and-masks

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

by (200 points)
selected by

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
        $PassThroughTimer.start()

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

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)

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.

I've created this right now as an example for you: Hope it helps.
https://drive.google.com/file/d/1aluHhINyLg1-YjxizQOt3YVCF36rJjsU/view?usp=sharing

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 :)

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.

Categories