How do I stop players from seeing through walls?

Alright, so I have a game where I want the player to be in first person and also see their body when they look down. When I do this, however, the torso takes up a large part of their field of view. To combat this, I set the camera offset a bit forward so they could only see the front of their body, and this is where the problem arose. Because of the camera being slightly closer to the wall, when the player pushes up against the wall the camera glitches inside of it and they can see past it to the other room. Is there anything at all that I can do to fix this issue?

Code:

local player = game.Players.LocalPlayer
local char = player.Character

char.Humanoid.CameraOffset = Vector3.new(0, 0, -0.9)

for i, v in pairs(char:GetChildren()) do
	if v:IsA("BasePart") and v.Name ~= "Head" then
		
		v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
			v.LocalTransparencyModifier = v.Transparency
		end)
		
		v.LocalTransparencyModifier = v.Transparency
		
	end
end
3 Likes

This is just a little idea, but rather than offsetting the camera you could make the player’s body transparent on the client whenever the player looks down or at a certain angle - otherwise, make the body visible.

Do a raycast from the head out to where you want your camera to be. After doing the raycast, set the camera offset to the difference between the hit position and the origin.

1 Like

Sorry I have no idea how to use raycasting. How would I do this?

He wants the body to be seeable, so making the torso transparent isn’t the solution.

2 Likes