Parts along edges of screen?

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.

Current behaviour:

What I want:

local Camera = workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()
local leftPart = Instance.new("Part")
leftPart.Anchored = true
leftPart.CanCollide = false
leftPart.Size = Vector3.new(0.05,1000,1000)
leftPart.Parent = workspace

game:GetService("RunService").RenderStepped:Connect(function()
    local leftUnitRay = Camera:ScreenPointToRay(0, Mouse.ViewSizeY/2,10)
    local leftRay = Ray.new(leftUnitRay.Origin, leftUnitRay.Direction * 20)
    leftPart.CFrame = CFrame.new(leftRay.Origin)*(Camera.CFrame-Camera.CFrame.p)
end)
1 Like

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.

1 Like

Yeah, but how do I get the direction off to the very left of the screen as shown by the arrow in the second picture?

What do you use to get the center?

Another edit: Sorry I’m an idiot, you already listed that.

Have you tried using the camera cframe’s other vectors like UpVector? Or you could use CFrame.Angles if you just need to re-orient it.

Bump, is it not possible to somehow make a cone of parts which represents the field of view?

It sure is possible!


-- 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 :frowning: 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.