I’m trying to make a ScreenGui frame cover a part so it’s not visible, the positioning is fine but i can’t get the sizing to work.
Code:
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local frame = script.Parent
local part = workspace:WaitForChild("Part")
runService.Heartbeat:Connect(function()
local screenPoint, isInBounds = camera:WorldToScreenPoint(part.Position)
if not isInBounds then
frame.Visible = false
return
end
frame.Visible = true
local dist = (camera.CFrame.Position-part.Position).Magnitude
frame.Size = UDim2.new(0,part.Size.X*dist,0,part.Size.Y*dist) --??
frame.Position = UDim2.new(0, screenPoint.X-frame.Size.X.Offset/2, 0, screenPoint.Y-frame.Size.Y.Offset/2)
end)
If you want it to be precisely around the part, I would start by getting the positions of all the 8 corners of the part, then doing Camera:WorldToScreenPoint(pos) on all of them to get the position on the screen and then draw a box that would contain them all. If you want the box to be rotated, then things get a bit more complicated, but I’m gonna assume that that isn’t what you want at the moment. To get that box, you’d simply get the left-most, up-most, right-most and down-most position of all of them, which, when combined, give you the top-left and bottom-right coordinates of the box. The size of the box in pixels would then just be bottomRightPos - topLeftPos, where the positions are made up of pixels (Offset in Udims), not scale. And you would probably need to add additional code in case some points aren’t on the screen, or are even behind the screen.