How to make text only appear when in an Area 2D Node and fade away once exited?

Hey I’m new to Godot and was wondering how I could make it so that when I approached a sign it would display text in the world above the sign and once I left the area it would fade away instead of immediately disappearing. Also I’m working in 2D and assuming an Area2D node is what I’ll need.

Thanks in advance for any help I really appreciate it! =)

You can use a Tween to do that easily, it’s a super useful class.

See my little demo of how this can be achieved:

Here’s a scene structure:

This is the code on the Sign node:

extends Area2D


@export var label: Label
var tween_duration: float = 0.5


func _ready() -> void:
	body_entered.connect(_on_body_entered)
	body_exited.connect(_on_body_exited)
	label.modulate = Color.TRANSPARENT


func _on_body_entered(body: Node) -> void:
	var tween: Tween = create_tween()
	tween.tween_property(label, "modulate", Color.WHITE, tween_duration)


func _on_body_exited(body: Node) -> void:
	var tween: Tween = create_tween()
	tween.tween_property(label, "modulate", Color.TRANSPARENT, tween_duration)

This is the code on the Player node, just so I can move in and out of the area to record the demo:

extends CharacterBody2D


var speed: float = 300.0


func _process(delta: float) -> void:
	var movement_direction: Vector2 = Vector2.ZERO
	movement_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
	
	velocity = movement_direction * speed
	move_and_slide()
1 Like

Hey thanks for the help. I’ve tried implementing your code but the project crashes and gives errors. I’m not sure what’s wrong. All my nodes are the same one’s as yours except for a sprite2D node. Also sorry for the late reply.

Here is a screenshot of the errors

You connected the signal through the inspector, but the signal is already connected through code here. You can’t have both - so either remove the signal connection from the inspector, or remove these 2 lines from the code.

	body_entered.connect(_on_body_entered)
	body_exited.connect(_on_body_exited)
1 Like

Alright I think I’ve remove the extra signal connection but now receive a new error. Apologies if I’m just being stupid.

Don’t worry, everyone learns :slight_smile:
You need to assign your Label node to the exported label variable.

1 Like

Good news the game doesn’t crash anymore :slightly_smiling_face:! Bad news nothing happens :melting_face:

Can you show a video or something?
What type of Node is your player?

1 Like

Yeah sure thing. Here is player and player script



I see your Player is in Collision Layer 2, and probably your Sign Area has Collision Mask 1, which means it will not be reporting any collisions with the player. You need to set you area to Collision Mask 2

1 Like

Got it working now! Thank you I really appreciate your help

1 Like