Player first person camera can see through walls

  1. What do you want to achieve? Keep it simple and clear!
    I wanted to make player’s first person camera not glitching/seeing through walls.
  2. What is the issue? Include screenshots / videos if possible!

robloxapp-20240521-1105293.wmv (1.9 MB)

It clips through the camera when player hits a wall.
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I looked up on Forum, there is a solution using Raycast, but I am still confused.

This is the cameraoffset

Humanoid.CameraOffset = Vector3.new(0, 0.5, -0.7)

Try this:

local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character and character:FindFirstChildOfClass(“Humanoid”)

if humanoid then
local camera = game.Workspace.CurrentCamera
local cameraOffset = humanoid.CameraOffset

local function updateCamera()
	local raycastResult = workspace:Raycast(
		camera.CFrame.Position,
		camera.CFrame.LookVector * cameraOffset.Z,
		{character}
	)

	if raycastResult then
		local distanceToObstacle = (raycastResult.Position - camera.CFrame.Position).Magnitude
		local minDistance = cameraOffset.Z * 0.5

		if distanceToObstacle < minDistance then
			local newCameraPosition = raycastResult.Position - camera.CFrame.LookVector * minDistance
			camera.CFrame = CFrame.new(newCameraPosition, raycastResult.Position)
		end
	end
end

camera:GetPropertyChangedSignal("CFrame"):Connect(updateCamera)

end

The error says Unable to cast Array to RaycastParams.

I don’t really know how Raycasting work, can you explain me how this work?

just change the local function updatecamera to this.

local function updateCamera()
	local raycastResult = workspace:Raycast(
		camera.CFrame.Position,
		camera.CFrame.LookVector * cameraOffset.Z,
	)

It does work, but not when my Humanoid Camera is a bit forward. Is there a way to block my humanoid camera when it reach the walls?

Maybe this can solve your problem: How to avoid camera clipping through walls?

Oh, I got it. but in my game the wall is not a custom mesh tho. Maybe there is the only way that i can do is set my humanoid camera backward a little bit.

Yeah you could do that, but it might be because of the walls’ transparency? Are they all 0?

yes, it’s all 0. But does they really affect?

Yeah you see if your transparency is above 0 your camera can go through walls.

it’s all 0, i have no idea lol

Yeah I have no idea what to do either, I have my own problem where my viewmodel is pushing the character even though collisiongroups are off and it’s massless…

oh, have you turn off the cancollide?

Yep, all types of collisions are off. Here’s the topic if you’re interested: