How to offset a BillboardGui so it's at the center of the screen

You can write your topic however you want, but you need to answer these questions:
So about an hour ago I posted an unfinished version of this post and I feel really dumb. I was going to just edit it, but at this point no one would see it so I’m just reposting this.

So I wanted to have a ScreenGui behind a part, but that’s not possible as far as I’m aware so I just decided to use a BillboardGui and offset it using SizeOffset. The closest I’ve got so far is the top right corner being centered.

I’ve tried to add, subtract, multiply and divide the scale, but when I try it just gets worse.

Here’s the script:

local cam = workspace.CurrentCamera
local part = script.Parent.Adornee

screenX = 0
screenY = 0

-- BillboardGui is script.Parent
script.Parent.Parent:WaitForChild("ScreenSize").Changed:Connect(function(property)
	if property == "AbsoluteSize" then

		local screensize = script.Parent.Parent:WaitForChild("ScreenSize")
                -- screensize is a ScreenGui in the player's gui
		screenX = screensize.AbsoluteSize.X
		screenY = screensize.AbsoluteSize.Y + 36

		local newscale = UDim2.new(0, screensize.AbsoluteSize.X, 0, screensize.AbsoluteSize.Y + 36)
		script.Parent.Size = newscale
	end
end)

-- ignore this part
local screensize = script.Parent.Parent:WaitForChild("ScreenSize")
screenX = screensize.AbsoluteSize.X
screenY = screensize.AbsoluteSize.Y + 36
local newscale = UDim2.new(0, screensize.AbsoluteSize.X, 0, screensize.AbsoluteSize.Y + 36)
script.Parent.Size = newscale


cam.Changed:Connect(function(property)
	if property == "CFrame" then

		local billboardPosition = script.Parent.Adornee.Position
		local vector = cam:WorldToViewportPoint(billboardPosition)
		
		local scaleoffset = Vector2.new(vector.X / screenX, vector.Y / screenY)
		scaleoffset = Vector2.new(scaleoffset.X * -1, scaleoffset.Y -1)

		script.Parent.SizeOffset = scaleoffset
	end
 end)

I’m sorry if this post is bad I’m new to this.

BillboardGuis will always be centered based on their Adornee, you will need to CFrame the Adornee part to the center of the screen if you would like it to be centered.

An easier way to do this would be to
have a ViewPort rendering the part unless you need it to be a physical object.

2 Likes

After about another hour of testing I found out I just needed to do this:

local scaleoffset = Vector2.new(vector.X / screenX, vector.Y / screenY)
scaleoffset = Vector2.new(scaleoffset.X * -1, scaleoffset.Y - 0.5)
scaleoffset = Vector2.new(scaleoffset.X + 0.5, scaleoffset.Y)

And it works
https://gyazo.com/137ee801a06ba827a67586940167ea5a

Sorry for any inconvenience

6 Likes