I’m currently making a FPS system from scratch and I’m adding procedural recoil.
The recoil needs to be applied in the opposite direction the camera is facing
The issue is that upon testing, the recoil is applied in one direction the entire time, Meaning if im facing that direction the recoil works, but if i turn 90 degrees its applied sideways and 180 degrees the recoil is applied backwards
I’ve tried updating the camera every frame but that’s obviously not the issue. I don’t know much about how the camera works yet.
Im currently using the spring module to make this
Current code: (in a module)
function ViewModelController:UpdateViewModelPos(dt)
for i, v in pairs(Camera:GetChildren()) do
if v:IsA("Model") then
--Recoil
local recoilOffset = self.recoilSpring.Offset
local recoilDirection = -Camera.CFrame.LookVector.Unit
local targetRecoilCFrame = CFrame.new(recoilDirection * recoilOffset)
local recoilCFrame = lastRecoilCFrame:Lerp(targetRecoilCFrame, 0.1)
local rotationalRecoil = CFrame.Angles(math.rad(recoilOffset * 2), 0, 0) -- Adjust the factor as needed
--Update Viewmodel positions
v:SetPrimaryPartCFrame(Camera.CFrame * aimCF * bobOffset * idleCF * currentSway * recoilCFrame)
self:UpdateCameraShake()
lastRecoilCFrame = recoilCFrame
end
end
end
RunService.RenderStepped:Connect(function(deltaTime)
ViewModelController:RenderStepped(deltaTime)
end)
Issue should be the order in which you multiply the CFrames. CFrames are matrices, and matrix multiplication is not commutative, meaning that a*b is NOT the same as b*a.
Because you already have a lot of terms that’s all being multiplied together, this is gonna take a bit of trial and error to get it right. But I will tell you that the recoil should be among the first few terms.
The issue persists. I believe the problem lies in this:
local recoilDirection = -Camera.CFrame.LookVector.Unit
local targetRecoilCFrame = CFrame.new(recoilOffset * recoilDirection)
I don’t know if the issue is with the camera or being in first person or what. I have no idea
Edit: the issue is still the same. The direction doesnt update with where the camera actually is
All i had to do was apply the spring in the Z direction.
heres the code if anyone has the same problem
local cameraCF = Camera.CFrame
local aimedCF = aimCF -- aimCF is relative to camera, so it's used directly
local idleCF = idleCF -- idleCF is relative to aimedCF
local swayCF = currentSway -- currentSway is relative to idleCF
local recoilOffset = self.recoilSpring.Offset
local recoilDirection = Vector3.new(0, 0, recoilOffset)
local recoilCFrame = CFrame.new(recoilDirection)
local finalCF = cameraCF * aimedCF * idleCF * swayCF * recoilCFrame * bobOffset
-- Update viewmodel positions
v:SetPrimaryPartCFrame(finalCF)