• Godot Help
  • Working on a hiding mechanic in a 2D game.

I am working on a 2D action game where I would like the player to sometimes hide in areas in the background. When I do that, I'd like the following things to happen

1) Take away all control inputs from the player, except moving down to come out of hiding
moving is done using the following lines of code
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()

and
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

2) remove collision with enemies, but only enemies and not the floor so the player doesn't fall through the floor
I tried
get_node("CollisionShape2D").disabled = true
but this obviously removes all collision, including with the floor and my character just falls off the screen

What would be the best course of action to implement these elements? any help is appreciated

for that first one what I'd do is make a hiding variable that flips between true and false when hiding or not hiding, then change if direction: to if direction and not hiding:

for the second I'd most likely use collision layers. removing the enemy collision layer from the player is probably the easiest way to accomplish that

    samuraidan Thanks for the reply. I agree with you on the first one. I already made an is_hiding value, good to know I can associate it with the directions. Quick Edit: This worked amazingly, thank you very much

    for the second one, I was afraid you'd say that. when you mean the collision layer, you're talking about the mask, correct? unfortunately, on the main character, I have him only collide and mask on layer 1. On my enemies I have a "PlayerDetector" Area2D that masks onto the player's layer. I guess I'll have to rework that.