How to detect if an external face of a wall is visible by the camera?

I have 4 parts that are 4 walls.
All walls are rotated correctly on the Y-axis: 0, 90, 180, -90, that is, their front face is pointing outwards:

In the example above, two walls have their external faces visible (by the current camera position/direction): left wall and front wall.

I’m trying this test to detect the left wall, but I’m getting only nil:

local Ray = workspace:Raycast(workspace.Camera.CFrame.Position, workspace.PartLeft.Position)
print(Ray) -- prints nil

How could I correctly detect these external faces?

The second argument of WorldRoot:Raycast is a directional vector, not a position vector.

You can do something like

local origin = workspace.CurrentCamera.CFrame.Position
local direction = (workspace.PartLeft.Position - origin).Unit
workspace:Raycast(origin, direction*50) -- check for part 50 studs

to get a vector in the direction of the left part. If you just want to check that it’s within the boundaries of the camera then use Camera:WorldToViewportPoint. The example at the bottom uses the old raycasting API, just make sure to convert that to use the new one.

1 Like