0 votes

I'm currently having issues trying to implement a door open command. the idea is that when the player enters the door's collision, it emits a signal that will then run an if statement to see if the player is pressing the open key. The code is shown below:

func _on_Door_body_entered(body):
    if Input.is_action_pressed("open"):
        GameState.next_level()

The issue is that it will allow the game to load the next scene, but only if the player is holding the open key prior to entering the door's collision shape. I believe (i'm still new so please correct me) that the issue is that the onDoorbodyentered signal is only being sent when the player first enters the collision shape, but not while the player is still colliding with it. Is there a way for me to have the door constantly emit a signal while the player is in its area, or should i find a different method for checking this? any help would be greatly appreciated.

asked May 2 in Engine by Andromachus (23 points)

1 Answer

+1 vote

You're right. onbodyentered is only called when the body enters the collider.
what you can do is

var is_inside := false

func on_body_entered(body):
    is_inside = true

func on_body_exited(body):
    is_inside = false

func unhandled_input(event: InputEvent) -> void:
     if Input.is_action_pressed("open"):
         if is_inside:
             GameState.next_level()
answered May 2 by Omar13 (164 points)
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.