0 votes

Hello,
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 :(

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

PS
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 :) please note that Im a total newbie.

asked May 24 in Engine by grindmybody (15 points)

1 Answer

+1 vote
Best answer

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

answered May 24 by kidscancode (16,966 points)
selected May 29 by grindmybody
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.