Collision masks configured via code

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

I have a platform 2D game with mechanism where the player can hide inside closet, under the bed ect. I set a global variable var playerHidden = false, if the player hides the flag will change for true.

During the hidden state, I need to turn off collisions Player-Enemy. And then turn it on again. How can I do it via code?

i tryed on my “Player”
$CollisionShape2D.disabled = true but it makes more bugs :frowning:

I set a collisions layers and masks, but I dont know how to change them via code…

During “hidden state” I use hide() and then show() for my player, when I turn on debug-show colisions, the colisions are not visible during hide() but there are still there coz the enemy is colliding with the player…

Main question: how to change collision mask/layer via code?

Please help :slight_smile: please note that Im a total newbie.

:bust_in_silhouette: Reply From: kidscancode

Disabling the collision shape is the normal way to do this.

As for layers/masks when you set them in the Inspector, if you hover over the property it shows you the property name. You can set that in code with collision_layer = or collision_mask = to whatever value you want.

You can also use set_collision_layer_bit() if you want to toggle a particular layer.

All of this comes from PhysicsBody2D

1 Like