VisibleOnScreenNotifier3D in Instantiated Objects

Godot Version



Pretty simple issue but one I haven’t been able to figure out a solution to.

Basically, I have a game where I’ve implemented text pop-ups. When the player clicks on a certain object, that object gets a new child Label3D with whatever text I’ve written.

I want to have behaviour where as soon as the player looks away from the Label3D the label gets removed from the game, so that when the player looks back at the object there’s no longer a pop-up there. However, seeing as I’m instantiating the Labels from script I can’t put the VisibleOnScreenNotifier3D node anywhere and set its bounding box to match the Label’s location. I could instantiate it as well, but the width/height of the labels can change quite a bit in between objects, so I wouldn’t know what value to set the AABB bounding box corners to.

Is there any alternative solution, or some obvious thing I’m missing that would allow this sort of behaviour? In general, just knowing how to instantiate the VisibleOnScreenNotifier3D from script (and set the bounding box to match whatever collider it’s a child of) would help a lot and allow me to accomplish a lot of what I’m looking for.

You can use VisualInstance3D.get_aabb() to get the AABB of the Label3D and assign that to the VisibleOnScreenNotifier3D.aabb property. You may need to await a frame to get the correct AABB after changing the text of the Label3D like:

func _ready():
	visible_on_screen_notifier_3d.screen_entered.connect(func(): print("hello"))
	visible_on_screen_notifier_3d.screen_exited.connect(func(): print("bye"))
	label_3d.text = "Hello world"
	await RenderingServer.frame_post_draw
	visible_on_screen_notifier_3d.aabb = label_3d.get_aabb()
1 Like