Hi, im trying to make a good and clean weapon sway but I don’t know If I have achieved this effect.
can you help me?.
this is my current code for the effects:
local function bindRenderStep(renderName)
local function bob(addition, magnitude, frequency)
return math.sin(tick() * addition * frequency) * magnitude
end
local lastSway = Vector3.new(0, 0, 0)
local lastBob = Vector3.new(0, 0, 0)
local lastBreathingOffset = Vector3.new(0, 0, 0)
local lastTiltAngle = 0
local swayFactor = 0.6
local swayScalingX = 0.003
local swayScalingY = 0.003
local bobMagnitude = 0.5
local bobMagnitudeAiming = 0.1
local bobFrequency = 1
local breathingAmplitudeX = 0.03
local breathingAmplitudeY = 0.01
local breathingFrequency = 1
local tiltAmount = 4
local lerpSpeed = 10
RuS:BindToRenderStep(renderName, Enum.RenderPriority.Camera.Value + 1, function(dt)
for _, v in pairs(Cam:GetChildren()) do
if v:IsA("Model") then
local delta = UIS:GetMouseDelta()
local humanoid = Char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local moveDirection = humanoid.MoveDirection
local cameraRight = Cam.CFrame.RightVector
local swayX = swayFactor * delta.X * swayScalingX
local swayY = swayFactor * delta.Y * swayScalingY
local updatedBob = Vector3.new(
bob(5, IsAiming and bobMagnitudeAiming or bobMagnitude, bobFrequency),
bob(10, IsAiming and bobMagnitudeAiming or bobMagnitude, bobFrequency),
bob(5, IsAiming and bobMagnitudeAiming or bobMagnitude, bobFrequency)
) / 15 * (Char.PrimaryPart.AssemblyLinearVelocity.Magnitude) / 15
local breathingOffsetX = math.sin(tick() * breathingFrequency) * breathingAmplitudeX
local breathingOffsetY = math.sin(tick() * breathingFrequency * 2) * breathingAmplitudeY
local breathingOffset = Vector3.new(breathingOffsetX, breathingOffsetY, 0)
local tiltAngle = -tiltAmount * moveDirection:Dot(cameraRight)
local smoothSway = lastSway:Lerp(Vector3.new(swayX, swayY, 0), lerpSpeed * dt)
local smoothBob = lastBob:Lerp(updatedBob, lerpSpeed * dt)
local smoothBreathingOffset = lastBreathingOffset:Lerp(breathingOffset, lerpSpeed * dt)
local smoothTiltAngle = lastTiltAngle + (tiltAngle - lastTiltAngle) * lerpSpeed * dt
lastSway = smoothSway
lastBob = smoothBob
lastBreathingOffset = smoothBreathingOffset
lastTiltAngle = smoothTiltAngle
local newCamCF = viewModel.FakeCam.CFrame:ToObjectSpace(viewModel.HumanoidRootPart.CFrame)
local updatedCFrame = (Cam.CFrame * CFrame.new(newCamCF.Position)) *
CFrame.new(smoothSway.X, smoothSway.Y, 0) *
CFrame.new(smoothBob.X, smoothBob.Y, 0) *
CFrame.new(smoothBreathingOffset) *
CFrame.Angles(0, 0, math.rad(smoothTiltAngle))
viewModel:PivotTo(updatedCFrame * AimCF)
end
end
if IsAiming and viewModel then
local offset = gunModel.Core.AimPart.CFrame:ToObjectSpace(viewModel.PrimaryPart.CFrame)
AimCF = AimCF:Lerp(offset, .1)
else
AimCF = AimCF:Lerp(CFrame.new(), .1)
end
end)
end
Video of Current Effects: