So to learn about screen space stuff I decided to put parts around the edges of the screen so it makes a cube around it and so far it works fine, but the part is rotated in such a way that it goes off into the middle of the horizon. How do I make it rotate so it follows the field of view line? Not sure how to explain it in words so here’s an illustration.
I would suggest doing the orientation using the camera’s cframe oriented toward the center before moving it to the side, instead of using it’s position from the side toward the center. If you are already doing this, then call me out.
Edit: I wish I saw that that wasn’t the problem before I posted. In this situation, you could orient it toward an altered center than the actual center.
-- BEWARE!!! Untested code ahead!
local PART_LENGTH = 100
local HALF_PART_LENGTH = PART_LENGTH / 2
local fromAngles = CFrame.Angles
local function getHorizontalFoV(camera)
return 2 * math.atan(0.5 * camera.ViewportSize.X / camera.NearPlaneZ)
end
local function getVerticalFoV(camera)
return camera.FieldOfView
end
local function getPerspectiveXClipCF(hFoV, origin)
return origin * fromAngles(0, hFoV/2, 0),
origin * fromAngles(0, -hFoV/2, 0)
end
local function getPerspectiveYClipCF(vFoV, origin)
return origin * fromAngles(vFoV/2, 0, 0),
origin * fromAngles(-vFoV/2, 0, 0)
end
local function getHeightZStudsOut(camera, zStuds)
return camera.ViewportSize.Y * zStuds / camera.NearPlaneZ
end
local function getWidthZStudsOut(camera, zStuds)
return camera.ViewportSize.X * zStuds / camera.NearPlaneZ
end
local camera = workspace.CurrentCamera
local hFoV = getHorizontalFoV(camera)
local vFoV = getVerticalFoV(camera)
local rightClipCF, leftClipCF = getPerspectiveXClipCF(hFoV, camera.origin)
local topClipCF, bottomClipCF = getPerspectiveYClipCF(vFoV, camera.origin)
local height = getHeightZStudsOut(camera, PART_LENGTH)
local width = getWidthZStudsOut(camera, PART_LENGTH)
local right = Instance.new 'Part'
right.CFrame = rightClipCF + (rightClipCF.LookVector * HALF_PART_LENGTH)
right.Size = Vector3.new(0.2, height, PART_LENGTH)
right.Parent = workspace
local left = Instance.new 'Part'
left.CFrame = leftClipCF + (leftClipCF.LookVector * HALF_PART_LENGTH)
left.Size = Vector3.new(0.2, height, PART_LENGTH)
left.Parent = workspace
local top = Instance.new 'Part'
top.CFrame = topClipCF + (topClipCF.LookVector * HALF_PART_LENGTH)
top.Size = Vector3.new(width, 0.2, PART_LENGTH)
top.Parent = workspace
local bottom = Instance.new 'Part'
bottom.CFrame = bottomClipCF + (bottomClipCF.LookVector * HALF_PART_LENGTH)
bottom.Size = Vector3.new(width, 0.2, PART_LENGTH)
bottom.Parent = workspace
Most of the code is self explanatory. It should be pretty close to what you need. I just found the vertical and horizontal fields of view (angles) and used it to find the CF looking parallel to the clip planes by rotating the camera origin CFrame by half the FoV (around the x or y axis) in localspace. I then used these 4 CFrames to find the desired positions of the parts by moving them forward 1/2 the part length studs (whatever each CFrame defines as forward). Hopefully I got everything right! (I’ve sworn off Windows, so I can’t run RobloxStudio without a lot of work and am unable to test it) If not, then it should be close to what you need. I can help debug it if need be on here.