Topic was automatically imported from the old Question2Answer platform.
Asked By
Zylann
Old Version
Published before Godot 3 was released.
How do I enable or disable a StaticBody2D from script?
I don’t see any auto-complete function that seem to do that. I tried show() and hide() but it does nothing.
It is a good question, as there doesn’t seem to be a way to disable the StaticBody2D node itself.
My workaround was to turn on the “Trigger” flag on the child CollisionShape2D. This will have the effect that kinematic and rigid bodies can pass through the static body. As the documentation states “A trigger shape detects collisions, but is otherwise unaffected by physics”.
Just turn this flag on to disable physics. For example, assuming you have a child CollisionShape2D to your StaticBody2D node, a script on the StaticBody2D would do something like:
var cs = get_node("CollisionShape2D") # get child collision shape node
cs.set_trigger(true) # turn the trigger flag on to disable physics
Note that the above code is not idiot-proof, and if you call enable before disable, it would loose collision forever (but you can fix it). That’s how it goes with example code :)
Since this question was asked, Godot 3 was released with a new feature; the “disable” variable on StaticBody2D / RigidBody2D. You can now disable a StaticBody2D like this:
var body = get_node("StaticBody2D")
body.disabled = true
This is incorrect. disabled is a property of CollisionShape2D, not the physics body.
kidscancode | 2018-04-12 21:04
@kidscancode is right, also, when restoring a CollisionShape2D via disabled = false it seems not to produce collisions anymore. I resorted to using the masks as per @Bojidar answer.