I have a scene that consists of a CSGBox
and a Label
as a child of it.
In the script for the CSGBox
, I get the label and center it on the box so that whenever the player is looking at it, they can see the label above the box.
extends CSGBox
# The box's label.
onready var label: Label = get_node("Label")
# The player's camera.
onready var camera: Camera = get_viewport().get_camera()
# Aligns the position of the label to always face the camera.
#
# `point` - The position to align the label to.
func position_label(point: Vector3):
var offset = Vector2(label.get_size().x / 2, 0)
label.rect_position = camera.unproject_position(point_2) - offset
# Every frame we check to see if the label is behind the camera or the label is
# out of the render distance and if so we return early. Otherwise, we position
# the label on the cube so that it's always facing the camera.
#
# `_delta` - The difference in time between the last frame and the current frame.
func _process(_delta):
var pos: Vector3 = global_transform.origin
# If the point is behind the camera we hide the label and return early.
if camera.is_position_behind(pos): return
position_label(pos)
This works pretty well, however it seems that the label gets smaller when moving closer to the box and larger when moving away from it. I know that it's not actually increasing in size because I can log the size of the text in the label and it's always the same but I'm not sure what could be causing this effect or how it can be "countered" to make the text seem the same no matter the distance.