NearPlaneZ is not correct for cameras other than CurrentCamera

My Roblox’s CurrentCamera NearPlaneZ is -.1 but other cameras have -.5 NearPlaneZ

And since NearPlaneZ is read-only, the camera methods such as WorldToViewportPoint are all inaccurate and useless with non CurrentCameras (which means we have to rewrite the methods ourselves but I don’t know all this linear algebra :sob:)

1 Like

I think those inaccurate results are caused by invalid ViewportSize. You can use the following script as a workaround.

local worldToScreenPoint = function(viewportFrame, worldPoint)

local camera = viewportFrame.CurrentCamera
local halfWidth = viewportFrame.AbsoluteSize.X / 2
local halfHeight = viewportFrame.AbsoluteSize.Y / 2
local pCamSpace = camera.CFrame:PointToObjectSpace(worldPoint)
local planeDistance = -halfHeight / math.tan(camera.FieldOfView * math.pi / 360)
local nPlaneY = pCamSpace.Y / pCamSpace.Z * planeDistance;
local nPlaneX = pCamSpace.X / pCamSpace.Z * planeDistance;
local visible = pCamSpace.Z < 0 and math.abs(nPlaneX) < halfWidth and math.abs(nPlaneY) < halfHeight
return Vector3.new(halfWidth + nPlaneX, halfHeight - nPlaneY, -pCamSpace.Z), visible 

end
3 Likes

Is NearPlaneZ always -.5 not a bug though?
Camera | Documentation - Roblox Creator Hub says it is -.5 on some devices, so it can’t just be because the viewportsize is 1,1

It is a bug. I will fix it. Thanks for your report.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.