I have been trying to make a sway system similar to COD (The one where the sway pivots around the handle if it is not COD and I’m wrong), but I can never get it to work. I have looked on websites, asked AI, but nothing seemed to work and my math is really bad.
The viewmodel when repositioning is offsetted and moves either forward or backwards when swaying side to side. The PrimaryPart is where the viewmodel pivots to the camera instead of the ACS framework where the handle is the primary part.
Here is the script I have been working on:
local function UpdateSway(delta)
-- // Framerate
local Speed = math.min(1, 60 * delta)
-- // Rot Calculation
local rot = CurrentCamera.CFrame:ToObjectSpace(LastCamCF)
local X, Y, Z = rot:ToOrientation()
local Max = 0.025
-- // Update spring
local swayX = math.clamp(math.sin(Y * 1.5), -Max, Max) * Speed
local swayY = math.clamp(math.sin(X * 1.5), -Max, Max) * Speed
local swayZ = 0
local sway = Vector3.new(swayY, swayX, swayZ)
local JointOffset = Vector3.new(4.409621715545654, 0.8226121664047241, 1.5469993352890015)
-- Calculate the sway rotation
local swayRotation = CFrame.Angles(sway.X, sway.Y, sway.Z)
-- Apply rotation to joint offset
local rotatedOffset = swayRotation:VectorToWorldSpace(JointOffset)
-- Calculate the offset CFrame
local OffsetCF = CFrame.new(rotatedOffset) * swayRotation * CFrame.new(-JointOffset)
if SwaySpringPos and SwaySpringRot then
SwaySpringPos.shove(OffsetCF.Position)
SwaySpringRot.shove(sway)
local SpringRot = SwaySpringRot.update(delta) * (IsAiming and 1/Framework.AimSpringDampening or 1)
local SpringPos = SwaySpringPos.update(delta) * (IsAiming and 1/Framework.AimSpringDampening or 1)
-- Calculate the final ViewmodelSwayCF
local finalSwayCF = CFrame.Angles(SpringRot.X, SpringRot.Y, SpringRot.Z)
ViewmodelSwayCF = finalSwayCF * CFrame.new(SpringPos)
end
LastCamCF = CurrentCamera.CFrame
end