Scaling a gui element based on the distance from the camera to the world point

Hello!

I am trying to have a gui scale based on the distance from the camera’s position to a world point(a vector3)
Basically the closer your camera is to the point the larger the gui element is and farther the smaller it is

So far I have positioning of the gui down but this is the only hold up.

get the magnitude (distance) from the player’s head, rootpart, etc and the point in your world.

If your script is a localscript and inside the gui:

local frameToScale = script.Parent.Frame
local worldPoint = game.Workspace.guiPoint --can be an object, vector3, etc. Just make sure that you convert to a vector3 at some point
local maxDistance = 50 --The distance at which the gui stops getting bigger
local maxSize = 0.9 --The maximum size of the frame in either direction (scale)

--get the player and it's character
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character

--make sure character exists and waits till it does
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end



--you can use heartbeat or something here, i'm writing pseudo-code and cant remember how to do that right now. in real code, while true do should **NOT** be used

while true do
	--get the distance between the part and humanoidRootPart
	local magnitude = math.abs((character:WaitForChild("HumanoidRootPart").Position - worldPoint.Position).Magnitude)

	--clamp magnitude to the maxDistance
	magnitude = math.clamp(magnitude,0,maxDistance)

	--translate the distance into a scale
	local scale = math.clamp(1-(magnitude/maxDistance),0,maxSize)

	frameToScale.Size = UDim2.new(scale,0,scale,0)

	wait(0.01)
end

You said you already had positioning down, so you could probably tie this into it.

It seems like you’re trying to make a custom billboard GUI.

1 Like

Thanks!

Essential yes.

It seems like you’re trying to make a custom billboard GUI.

If your wondering what this is for, I’m making a personal attachment editor plugin. :slight_smile:

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