I've been making a custom camera and this code runs per frame:
RunService.RenderStepped:Connect(function()
UserInputService.MouseIconEnabled = false
if not char:FindFirstChild "UpperTorso" then return end
local ray = cam:ViewportPointToRay(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2, 0.5)
local dir = Vector3.new(ray.Direction.X, 0, ray.Direction.Z).Unit * FORWARD_OFFSET
--[[local raycast = workspace:Raycast(char.UpperTorso.Position, ray.Direction.Unit * FORWARD_OFFSET, params)
if raycast and raycast.Instance.Transparency < 1 then
print(raycast.Instance.Name)
dir = Vector3.new(0)
end]]
local rx, _, rz = char.UpperTorso.CFrame:ToOrientation() -- mild camera shake based on animation
local _, ry2, _ = char.HumanoidRootPart.CFrame:ToOrientation() -- excepting the y-axis
angley = lerpAngle(angley, ry2, LERP_SPEED) -- makes sure the angle doesn't accidentally wrap round and disorient the heck out of the player
cam.CFrame = (CFrame.new(char.UpperTorso.Position) + dir) * (CFrame.Angles(rx*CAMERA_SHAKE, angley, rz*CAMERA_SHAKE)) * CFrame.new(0, 2, 0) * CFrame.Angles(angle, 0, 0)
local d = UserInputService:GetMouseDelta()
char:PivotTo(char.PrimaryPart.CFrame * CFrame.Angles(0, -math.rad(d.x), 0))
target_angle = math.clamp(target_angle - math.rad(d.y), math.rad(-60), math.rad(60))
angle = math.lerp(angle, target_angle, LERP_SPEED)
end)
as you can see, I've already tried to move the camera back when the player faces a wall (which has been commented out), however when looking too far up / down, the camera clips through anyway.