I'm guessing the issue is that the _on_player_PlayerInBouds
function is only called once the player enters the Area2D. This means that while the player is in the Area2D, the _on_player_PlayerInBouds
function will not be called, because it is only called once the player enters the Area2D.
To fix this, you just need to use a boolean and move the condition to either _process
or _physics_process
, like this:
var is_player_in_area = false
func _process(_delta):
if is_player_in_area == true:
if Input.is_action_pressed("pickup"):
destroy()
func _on_player_PlayerInBouds():
# I'm assuming this function is connected through a signal
# in the Godot editor or something...
is_player_in_area = true
# NOTE: you will also need to connect the signal
# for when the player leaves the Area2D and in
# that function set "is_player_in_area" to false.
# Otherwise, the player can leave the area and still
# interact with the object.
func destroy():
# play sound
queue_free()
If I understand the issue correctly, this should fix it. As I mentioned in the comments, you will need to connect the signal for when the player leaves the area, and set is_player_in_area
to false
when the player leaves the area, otherwise the player will be able to interact with the object after entering the Area2D, even if they are no longer in the Area2D anymore.
Also, welcome to the forums!