Hello everyone. Faced a problem when the player’s camera passes through walls. Please help me fix this
RunService.RenderStepped:Connect(function()
local isFirstPerson = (camera.CFrame.Position - head.Position).Magnitude < 1.6
if isFirstPerson then
humanoid.CameraOffset = Vector3.new(0,0,-1)
else
humanoid.CameraOffset = Vector3.new(0,0,0)
end
for _, v in character:GetChildren() do
if v:IsA("BasePart") and v ~= head then
v.LocalTransparencyModifier = 0
end
end
end)
Here is the script
The problem is that you are offsetting the camera to at the front of the player’s head so collisions won’t prevent the camera from clipping into the surface. To avoid this you are going to have to push the camera backwards a little bit, precisely the Camera’s NearPlaneZ + some marginal distance away from the head collision box. So something like this:
if isFirstPerson then
humanoid.CameraOffset = Vector3.new(0,0,-1 + camera.NearPlaneZ + 0.1)
Note: having the hardcoded 1 offset might break this for differing head collision box sizes.
Another option would be to just make the head collision box larger if you want to maintain the effect of having the camera in front of the character.
If I do this, then the player’s body will not be visible