How do I make a label appear upon entering Area2D?

Godot Version

4.2.2

Question

Hey, I am trying to make a Label appear whenever the player enters an Area2D. I want my Label to be hidden from the beginning and only show when the player is in the area. My issue is that the text is there before the player enters it. When the player enters the area and then leaves, the text disappears as well. Can someone help me out with this?

Code

extends Area2D

var entered = false

@onready var enterdungeontext = $enterdungeonlabel


func _ready():
	enterdungeontext.visible = false

func _on_body_entered(_body):
	enterdungeontext.visible = true
	entered = true

func _on_body_exited(_body):
	enterdungeontext.visible = false
	entered = false
	
func _physics_process(_delta):
	if entered == true:
		if Input.is_action_just_pressed("switch-maps"):
			get_tree().change_scene_to_file("res://scenes/tutorial_dungeon.tscn")

If you only want it to show when the player enters then you need to add a condition to check for the player.

func _on_body_entered(body):
	if body is Player:
		enterdungeontext.visible = true
		entered = true

Chances are some other body is entering the area, like a static body such as tilemap tiles.

Hey, your reply helped me out! I didn’t exactly follow your instructions, but I managed to find a way to solve it that uses the same logic!

Code

extends Area2D

var entered = false

@onready var enterdungeontext = $enterdungeonlabel
@onready var player = $"../player" # made a variable that references to the player



func _ready():
	enterdungeontext.visible = false

func _on_body_entered(body):
	if body == player: # this checks if it's the player entering the Area2D
		enterdungeontext.visible = true
		entered = true

func _on_body_exited(_body):
	enterdungeontext.visible = false
	entered = false
	
func _physics_process(_delta):
	if entered == true:
		if Input.is_action_just_pressed("switch-maps"):
			get_tree().change_scene_to_file("res://scenes/tutorial_dungeon.tscn")
		
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.