Camera Troubles

Okay so I made a custom shiftlock script, basically it just locks your camera to the center of the screen and locks you zoomed out.

I had a bug where you could look through walls since it was forcing you to be zoomed out, so I made a raycast and changed the zoom distance based off the raycast distance.

Now the problem I am facing is that the camera is still technically inside the object, and so the object becomes invisible. I cannot just make the camera zoom in slightly more since the limit is 0.15 for camera zoom, if anything is closer than it just doesn’t work.

The solution I thought of is to just move the camera forward slightly, the problem is I got no clue how to do that.

local direction = ((raycastResult.Position - camera.CFrame.Position).Unit * 5)
camera.CFrame = camera.CFrame + camera.CFrame.LookVector * direction

this is what I tried, it didn’t work.

Btw for anyone interested, the problem still stands.

If I’m correct, the “normal” of an instance is the direction of the face the ray hits is looking.

We can use this normal variable to push the camera slightly in the opposite direction of the object the camera is hitting. But now that I think of it, if we push the camera away, then it will autozoom out to the maximum zoom distance, recreating the loop, which might cause the screen to shake forward and backwards.

A video of the problem :

cant you just do camera.CFrame += ray.Normal?. not the best with cframes when it comes to adding them with vectors but wouldnt that work?

Didn’t work, I ended up having to run a loop on renderstepped (I don’t know why this is the case, but its the solution.

To anyone having my problems, take my code for free. This goes in a localscript inside startercharacterscripts.

local runs = game:GetService("RunService")

runs.RenderStepped:Connect(function(deltaTime)
	local player = game.Players.LocalPlayer
	local character = player.Character
	local head = character:WaitForChild("Head")
	local camera = workspace.CurrentCamera

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {character, camera}
	params.FilterType = Enum.RaycastFilterType.Exclude
	local raycastResult = workspace:Raycast(head.Position, (camera.CFrame.Position - head.Position), params)

	if raycastResult then
		camera.CFrame = (camera.CFrame - (camera.CFrame.Position - raycastResult.Position)) + (character.Head.Position - camera.CFrame.Position).Unit    
	end
end)

Without my script :

With my script :

1 Like

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