So, I’ve got this script here i made to keep players cameras from clipping through walls.
The script works great, but it interferes with a movement script i made which lets the player slide and uses camera offset to lower the camera.
local RunService = game:GetService("RunService")
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Camera = game.Workspace.CurrentCamera
local Head = Character:FindFirstChild("Head")
local HumRootPart = Character:WaitForChild("HumanoidRootPart")
Humanoid.CameraOffset = Vector3.new(0, 0, -0.8)
local OriginalCameraOffset = Humanoid.CameraOffset
local function TweenCamera(SetPosition)
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.4, Enum.EasingStyle.Exponential)
local Goal = {CameraOffset = SetPosition}
local Tween = TweenService:Create(Humanoid, Info, Goal)
Tween:Play()
end
local function FixCameraClip()
local IgnoreParts = {}
for _, part in pairs(game.Workspace:GetChildren()) do
if (part:IsA("BasePart") or part:IsA("MeshPart")) and part.Transparency == 1 then
table.insert(IgnoreParts, part)
end
end
for _, part in pairs(Character:GetDescendants()) do
if part:IsA("BasePart") or part:IsA("MeshPart") then
table.insert(IgnoreParts, part)
end
end
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = IgnoreParts
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local Raycast = game.Workspace:Raycast(Head.Position + OriginalCameraOffset, Head.CFrame.LookVector * 1.7, rayParams)
if Raycast and Humanoid.CameraOffset.Z < -0.4 then
print(Raycast.Distance)
print(Raycast.Instance)
local NewPosition = Vector3.new(0, 0, -0.4)
TweenCamera(NewPosition)
elseif not Raycast then
TweenCamera(OriginalCameraOffset)
end
end
local Connection = RunService.RenderStepped:Connect(function ()
FixCameraClip()
end)
How could i fix this?