hey guys so i want the player to have a gui on a part by using worldToScreenPoint so it shows where a part is on the screen using a gui.
The way it would work is that it converts the world to a screenpoint so the gui is position at the position.
I can’t use billboard as they stop rendering at a certain distance even when set to inf.
So is there a code to make worldtoscreenpoint work with gui since i dont know anything about WorldToScreenPoint
WorldToScreenPoint returns two values. the first one is a Vector3. It’s X and Y tell where the world position is on the screen. They tell it as pixels from the top left corner of the gui space, which includes most of the screen, but not the top bar. The Z in the vector tells the depth of the world point from the screen as studs. The second return value is a bool that tells if the part is on the screen. If you want to calculate the position using the whole viewport, you can use WorldToViewportPoint instead. It behaves mostly the same way as WorldToScreenPoint, it just uses the whole viewport. Anyways, here’s some example code.
local RunService = game:GetService("RunService")
local guiObject = -- the gui object that needs to be positioned
local part = -- the part used to position the guiObject
local camera = workspace.CurrentCamera
guiObject.AnchorPoint = Vector2.new(.5, .5)
RunService.RenderStepped:Connect(function()
local worldPos = part.Position
local pos, isOnScreen = camera:WorldToScreenPoint(part.Position)
if isOnScreen then
guiObject.Visible = true
guiObject.Position = UDim2.FromOffset(x, y)
else guiObject.Visible = false
end
end)