Keeping a part 100 studs in front of the camera at all times on the same Y level

Hi, I’m attempting to make my sea “move” 100 studs in front of where my camera is facing. This is my current code:

local Camera = workspace.CurrentCamera
local Sea = workspace:WaitForChild('Map'):WaitForChild('Ocean'):WaitForChild('Main1')

while true do -- Plan on replacing this with a renderstepped and heartbeat, just trying to get this to work
	wait()
	local LookAt = Camera.CFrame
	--Sea.CFrame = CFrame.new(Sea.CFrame.p, Vector3.new(LookAt.X, Sea.CFrame.p.Y, LookAt.Z * 200)) -- This solution didn't really work
	Sea.Position = Vector3.new(Camera.CFrame.Position.X, Sea.Position.Y, Camera.CFrame.Position.Z) -- Best solution, but doesn't do exactly what I want
end

What it does (displayed) and where I want it (blue line):

I’m attempting to solve this issue where my billboardguis and bubble chat is rendering behind the slightly transparent sea by making sure the center of the sea is further back than where the camera is facing at all times, as @Corecii said.

I’ll be honest, I’m not very good with cframes and the math related with it. I’ve never fully understood it. Any help would be appreciated!

1 Like

Something like this?

local HORIZONTAL = Vector3.new(1, 0, 1)

--Returns (0, 0, 0) if vector is 0 length, vector.Unit otherwise.
--Helps with situations where you want to avoid getting (NaN, NaN, Nan) from vector.Unit
function unitOrZero(vector)
     if vector.Magnitude == 0 then
        return Vector3.new()
    end
    return vector.Unit
end

while true do
	wait()
	local LookAt = Camera.CFrame
	Sea.Position = Vector3.new(Camera.CFrame.Position.X, Sea.Position.Y, Camera.CFrame.Position.Z)
        --unitOrZero necessary when player looks straight up/down, which is never with default camera, but did it just to be safe.
        Sea.Position += unitOrZero(Camera.CFrame.LookVector * HORIZONTAL) * 100
end