I have created a code that sets the pivot to the CurrentCamera in Lua. I’m also considering implementing a raycast: if the raycast detects a collision, then the viewmodel will be pushed back.
camera_manipulation.viewmodel_Tocamera = function(viewmodel)
local firePart = viewmodel:FindFirstChild("Nodes", true).FirePart
local filterparams = RaycastParams.new()
filterparams.FilterDescendantsInstances = {currentCamera, viewmodel, player.Character}
filterparams.FilterType = Enum.RaycastFilterType.Exclude
viewModelToCamera = runService.RenderStepped:Connect(function()
local part = Instance.new("Part",workspace)
part.Anchored = true
part.CanCollide = false
part.CFrame = firePart.Parent.Parent.PrimaryPart.CFrame
part.Size = firePart.Size
part.Transparency = 1
local rayResult = workspace:Raycast(part.Position,part.CFrame.LookVector * 5 , filterparams)
if rayResult then
print(rayResult.Instance.Name)
viewmodel:PivotTo(currentCamera.CFrame * CFrame.new(0,0,rayResult.Distance))
else
print("Not hitting anything")
viewmodel:PivotTo(currentCamera.CFrame)
end
part:Destroy()
end)
end