Prevent MouseLock camera from going through walls

Video of problem: https://youtu.be/PNKIW21doe4

I have been trying to edit the default CameraModule so that the camera does not clip through parts while mouse lock is enabled.

The problem is that the popper module uses the camera focus to decide whether or zoom the camera in. MouseLock sets the focus to next to your character. If the focus is inside the part, it will not detect anything between the focus and the actual camera position.

I tried editing the ZoomController and Popper modules to use your character’s head as the focus but it did not work.

How do I fix this?

3 Likes

Do mesh barriers work? Put a mesh inside of the part. It prevents cameras going through transparent parts without lock on, I wonder if it does the same for this issue.

Well whenever you have camera lock on, or you make your own 3rd person camera system, it doesn’t automatically prevent players from seeing through walls…

If the player is in the orbital type of camera (seen in most games), then the player won’t be able to see through CanCollide = true objects (most of the time) - which is where your idea was going (:

I tried following this post: https://devforum.roblox.com/t/stop-shift-lock-looking-through-walls/27601/3

I added this code after line 126 in the ClassicCamera module.

local offset = self:GetMouseLockOffset()
local part, position = workspace:FindPartOnRayWithIgnoreList(
	Ray.new(subjectPosition, (newCameraFocus.Position-subjectPosition).Unit * offset.Magnitude),
	{player.Character}
)
offset = offset - (newCameraFocus.Position-position)

The camera still goes through walls.

Update:

In the Poppercam module, on line 96, if you change

local zoom = ZoomController.Update(renderDt, rotatedFocus, extrapolation)

to

local zoom = ZoomController.Update(renderDt, game.Players.LocalPlayer.Character.Head.CFrame, extrapolation)

it works somewhat, the camera won’t go into the part as much, but it still does by about 2 studs? Changing the rotatedFocus variable to your character’s head cframe also breaks the poppercam when you are not in MouseLock.

I tried your method for a custom shoulder camera. It works.

I want to edit the default CameraModule modules so that MouseLock no longer goes through walls.

The problem is that I am not sure which module to implement the code you provided. The default modules are pretty confusing.

I can’t tell you exactly which module to edit off the top of my head, but you can figure this out yourself. I would try to find the camera update loop that runs when the MouseLock is on. Just read through the modules and try to see track down the loop that runs during mouse lock.

Once you find the camera update loop, you can mess with the code that sets the CFrame of the camera to make sure that it doesn’t go through walls.

I added these lines to after line 217 in the ClassicCamera module

local rootPart = self:GetHumanoidRootPart()
local characterPosition = rootPart.CFrame.Position + Vector3.new(0, humanoid.HipHeight, 0)
				
local part, position = workspace:FindPartOnRay(
	Ray.new(characterPosition, CFrame.new(characterPosition, newCameraCFrame.Position).LookVector * (newCameraCFrame.Position - characterPosition).Magnitude), 
	player.Character, false, true)
				
if part then
	position = (CFrame.new(position, CFrame.new(characterPosition, cameraFocusP).Position) * CFrame.new(0, 0, -0.5)).Position
end
				
newCameraCFrame = CFrame.new(position - (zoom * newLookVector), cameraFocusP)

It seem to have some weird affects.

I have an open source place if anyone wants to test it

So, I was trying to see whats wrong with the code and I was going to compare it to the default code. But when I disabled your modified modules and just used the default camera modules, turns out that shift lock already takes walls into account.

https://gyazo.com/6f370265392aa9a8a1e588d810d6945d

1 Like

Move the focus inside the wall. If the focus is in the wall, the camera will clip through parts.

The only change was the lines I added in the ClassicCamera module in my previous post.