Creating Weapon Sway

I’ve got a script here, it works great, but whenever it switches magnitude in the script it makes the weapon kinda snap, any way i could give it a smooth transition?

	local SetViewPosition = RunService.RenderStepped:Connect(function (dt)

		local CurrentCFrame = V_Viewmodel:GetPivot()

		if HumRoot.AssemblyLinearVelocity.Magnitude < 1 then

			t = 0
			
			local DefaultGunShake = math.sin(tick() * 2) * 0.05
			local TargetCFrame = Camera.CFrame * CFrame.new(0 + DefaultGunShake,-4.5 + DefaultGunShake, 0.55)
			print(TargetCFrame)
			V_Viewmodel:PivotTo(TargetCFrame)
			
		else
			t += dt
			local GunShake = math.sin(t * 5) * 0.1
			V_Viewmodel:PivotTo(Camera.CFrame * CFrame.new(0 + GunShake,-4.5 + GunShake, 0.55))

		end

	end)

bumping this, im kinda desperate.

bumping this topic I need help

I can’t understand what do you mean. Could you provide a video?

1 Like

Shotgun.wmv (2.4 MB)
Can you see how the weapon snaps when the player stops walking, and when they start walking?

use tweenservice?
(blablablablabla)

You could try to use Lerp, here’s an example:

local alpha = 0.1 -- change this on how fast you want the lerp to be ]0, 1]
local TargetCFrame = Camera.CFrame * CFrame.new(0 + DefaultGunShake,-4.5 + DefaultGunShake, 0.55)
V_Viewmodel:PivotTo(V_Viewmodel.PrimaryPart.CFrame:Lerp(TargetCFrame, alpha * dt * 60))
1 Like

so i anchored the torso because that was affecting it and then i raised it up to 0.99 because for some reason it was still bugging out, this is the result i got.
Shotgun (2).wmv (2.0 MB)

note: sorry if this is a lil confusing but its easier than creating animated sways for each state the gun is in

Tween the CFrame. Tweenservice is perfect here for smooth and scripted animations.

2 Likes

The thing is the tween wont update when the player moves so it kinda screws up the whole thing

so spawning a new task and having a loop inside of that doesn’t do anything?

Im confused on what you mean by this?

Im trying to get the weapon sway to look something like this:
Example.wmv (1.2 MB)
without relying on animations