i’m currently making a gun system that is straying from the more arcadey type and leaning over to the more realistic side like how escape from tarkov or insurgency:sandstorm does it, but i’m attempting to code a functionality where if the viewmodel gets too close/impacts a wall it will move back away from the wall and not clip through it but i’m extremely confused on how to go about doing it.
my current code partially works in my favor, but upon impact it keeps going back then immediately stops the raycast which then triggers it to return back and vice versa
vm:SetPrimaryPartCFrame(game.Workspace.CurrentCamera.CFrame * require(vm.Offset) * moveoffset)
game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame
local result = game.Workspace:Raycast(vm:WaitForChild(tool.Name):WaitForChild("Barrel").Position,vm:WaitForChild(tool.Name):WaitForChild("Barrel").CFrame.LookVector,viewmodelParams)
if result then
moveoffset = moveoffset:Lerp(moveoffset + Vector3.new(0,0,0.3),0.5)
else
moveoffset = moveoffset:Lerp(CFrame.new(0,0,0),0.5)
end