Help with fps camera collide when going backwards

  1. What do you want to achieve? Stop camera shaking/body colliding when moving backwards while crouching

  2. What is the issue? As seen in the video going backwards while crouching makes the camera shake or body colliding couldn’t figure out exactly

  3. Video of the issue Watch 2024-08-03 18-00-12 | Streamable

repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil

local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")

CanToggleMouse = {allowed = true; activationkey = Enum.KeyCode.R;}
CanViewBody = true
Sensitivity = 2
Smoothness = 0.2
FieldOfView = 80
HeadOffset = CFrame.new(0,0.1,0.5)

local cam = game.Workspace.CurrentCamera

local player = players.LocalPlayer
local m = player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388"
local character = player.Character or player.CharacterAdded:wait()
local human = character.Humanoid
local humanoidpart = character.HumanoidRootPart

local head = character:WaitForChild("Head")
local CamPos,TargetCamPos = cam.CFrame.p,cam.CFrame.p 
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

local running = true
local freemouse = false
local defFOV = FieldOfView

local w, a, s, d, lshift, crouching = false, false, false, false, false, false

local easingtime = 0.1
local walkspeeds = {
    enabled = true;
    walkingspeed = 16;
    backwardsspeed = 10;
    sidewaysspeed = 15;
    diagonalspeed = 16;
    runningspeed = 16;
	runningFOV= 80;
	crouchingSpeed = 8;
}

function updatechar()
    for _, v in pairs(character:GetChildren()) do
        if CanViewBody then
            if v.Name == 'Head' then
                v.LocalTransparencyModifier = 1
                v.CanCollide = false
                v.face.LocalTransparencyModifier = 1
            end
        else
            if v:IsA('Part') or v:IsA('UnionOperation') or v:IsA('MeshPart') then
                v.LocalTransparencyModifier = 1
                v.CanCollide = false
            end
        end

        if v:IsA('Accessory') then
            v:FindFirstChild('Handle').LocalTransparencyModifier = 1
            v:FindFirstChild('Handle').CanCollide = false
        end

        if v:IsA('Hat') then
            v:FindFirstChild('Handle').LocalTransparencyModifier = 1
            v:FindFirstChild('Handle').CanCollide = false
        end

        if v.Name == 'Torso' or v.Name == 'UpperTorso' or v.Name == 'LowerTorso' then
            v.LocalTransparencyModifier = 1
            v.CanCollide = true
        end
    end
end

function lerp(a, b, t)
    return a * (1-t) + (b*t)
end

input.InputChanged:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
        local delta = Vector2.new(inputObject.Delta.x/Sensitivity, inputObject.Delta.y/Sensitivity) * Smoothness
        local X = TargetAngleX - delta.y 
        TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X 
        TargetAngleY = (TargetAngleY - delta.x) %360 
    end    
end)

input.InputBegan:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then
        if inputObject.KeyCode == CanToggleMouse.activationkey then
            freemouse = not freemouse
        end
        if inputObject.KeyCode == Enum.KeyCode.W then
            w = true
        end
        if inputObject.KeyCode == Enum.KeyCode.A then
            a = true
        end
        if inputObject.KeyCode == Enum.KeyCode.S then
            s = true
        end
        if inputObject.KeyCode == Enum.KeyCode.D then
            d = true
        end
        if inputObject.KeyCode == Enum.KeyCode.LeftShift then
            lshift = true
		end
		if inputObject.KeyCode == Enum.KeyCode.C then
			crouching = true
		end
    end
end)

input.InputEnded:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then
        if inputObject.KeyCode == Enum.KeyCode.W then
            w = false
        end
        if inputObject.KeyCode == Enum.KeyCode.A then
            a = false
        end
        if inputObject.KeyCode == Enum.KeyCode.S then
            s = false
        end
        if inputObject.KeyCode == Enum.KeyCode.D then
            d = false
        end
        if inputObject.KeyCode == Enum.KeyCode.LeftShift then
            lshift = false
		end
		if inputObject.KeyCode == Enum.KeyCode.C then
			crouching = false
		end
    end
end)

runService.RenderStepped:connect(function()
	local stealth = character:FindFirstChild("stealth")
	if stealth then
		crouching = true
	else
		crouching = false
	end
    if running then
        updatechar()
        CamPos = CamPos + (TargetCamPos - CamPos) * 0.28 
        AngleX = AngleX + (TargetAngleX - AngleX) * 0.35 
        local dist = TargetAngleY - AngleY 
        dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
        AngleY = (AngleY + dist * 0.35) % 360
        cam.CameraType = Enum.CameraType.Scriptable
        cam.CFrame = CFrame.new(head.Position) 
            * CFrame.Angles(0, math.rad(AngleY), 0) 
            * CFrame.Angles(math.rad(AngleX), 0, 0)
            * HeadOffset

        humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0, math.rad(AngleY), 0)
    else
        input.MouseBehavior = Enum.MouseBehavior.Default
    end

    if (cam.Focus.p - cam.CFrame.p).magnitude < 1 then
        running = false
    else
        running = true
        input.MouseBehavior = freemouse and Enum.MouseBehavior.Default or Enum.MouseBehavior.LockCenter
    end

    if not CanToggleMouse.allowed then
        freemouse = false
    end

    cam.FieldOfView = FieldOfView

	if walkspeeds.enabled then
        if w and s then return end
        if w and not lshift then
            FieldOfView = lerp(FieldOfView, defFOV, easingtime)
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.walkingspeed, easingtime)
        elseif w and a then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.diagonalspeed, easingtime)
        elseif w and d then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.diagonalspeed, easingtime)
        elseif s then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.backwardsspeed, easingtime)
        elseif s and a then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.backwardsspeed - (walkspeeds.diagonalspeed - walkspeeds.backwardsspeed), easingtime)
        elseif s and d then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.backwardsspeed - (walkspeeds.diagonalspeed - walkspeeds.backwardsspeed), easingtime)
        elseif d then
            human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.sidewaysspeed, easingtime)
        elseif a then
			human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.sidewaysspeed, easingtime)
		elseif crouching then
			human.WalkSpeed = lerp(human.WalkSpeed, walkspeeds.crouchingSpeed, easingtime)
		end
    end
end)