0 votes

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

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.

in Engine by (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

by (18,221 points)
selected by
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.