Help with Hook's Law Sway on Gun

Hi,
I am making a Viewmodel and wanted to add Hooks law to it. I then followed @EgoMoose video explaining it. Problem is it doesnt work at all, not a single movement.

Scripts :

Module Script :

local spring = {}

function spring.new(Position, Velocity, Target)
	local self = setmetatable({}, {__index = spring})
	self.Position = Position;
	self.Velocity = Velocity;
	self.Target = Target
	self.k = 1;
	self.friction = 1;
	
	return self
end

function spring:update()
	local d = (self.Target - self.Position);
	local f = (d * self.k)
	self.Velocity = (self.Velocity * ( 1 - self.friction)) + f
	self.Position = self.Position + self.Velocity
end

return spring

Local Script :

local Hook = require(game.ReplicatedStorage.WeaponModules.Hook)
Spring = Hook.new(WeaponViewmodel.PrimaryPart.Position, Vector3.new(0, 0, 0), camera.CFrame.LookVector)
Spring.Friction = .1
-- rest of code

RunService.RenderStepped:Connect(function(deltaTime)
	if Spring then
		Spring.target = camera.CFrame.LookVector
		Spring:update()
		HookCF = CFrame.new(Spring.Position)
	end
-- rest of code again
end)

Thanks you for eventual answers!

1 Like

Dundidit.

Two methods posted, one with the fps framework below tutorial by BIackShibe and my method.

1 Like

Ok I’ll check that out ! Thanks

Now i have made this but I don’t know what to do with it. IF you could help me or indicate me what i could do !

Code :

if WeaponViewmodel then
		local Current, Velocity, Target  = WeaponViewmodel.PrimaryPart.CFrame.LookVector, Vector3.new(), camera.CFrame.LookVector
		
		deltaTime *= 60
		
		local k, friction = 0.005 * deltaTime, 0.175
		local PrimaryPartCFrame = WeaponViewmodel.PrimaryPart.CFrame
				
		local Distance = (Target - Current) 
		
		local Force = Distance * k
		Velocity  = (Velocity * (1 - friction)) + Force
	end

Thank you again !