How to stop character from clipping camera into walls

I’m trying to make it so that the player’s character is visible while in first person, but since the camera is offset, the camera clips into walls whenever a player walks up to one:

I’m trying to make it look something like this instead:

I’ve tried to fix this by using a Spherecast and CFraming the player back if the cast touches the wall. It sort of worked, but is very glitchy. Is there any way I can manage to achieve this?

Here is the code I used for this. It’s kinda sloppy, but it works for testing purposes:

local Character = script.Parent

for i, v in pairs(Character:GetChildren()) do
	if v:IsA("BasePart") and v.Name ~= "Head" then
		v.Changed:Connect(function()
			v.LocalTransparencyModifier = 0
		end)
	end
end

Character.Humanoid.CameraOffset = Vector3.new(0,0, -1)

local rp = RaycastParams.new()
rp.FilterType = Enum.RaycastFilterType.Exclude
rp.FilterDescendantsInstances = {Character}

game:GetService('RunService').RenderStepped:Connect(function()
	local castResult = workspace:Spherecast(Character.Head.Position, .5, Character.Head.CFrame.LookVector, rp)
	if castResult then
		Character.PrimaryPart:PivotTo(CFrame.new(Character.HumanoidRootPart.Position + (Character.HumanoidRootPart.CFrame.LookVector).Unit * (castResult.Distance - .45)))
	end
end)
3 Likes

Try changing the CameraOffset in the humanoid so it’s more behind.

1 Like

Try making a transparent wall in front of the wall. That way you are colliding with the transparent wall and your camera is not clipping through the visible wall.

1 Like

A massless collision box that is welded to the character could be a simple solution.

3 Likes

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