How do i make a viewmodel work with springs

viewmodels need springs or something idk saw it somewhere
tried adding springs to my viewmodel script and failed horribly

code :slight_smile:

Viewmodel movement

local VmSpring = Spring.newSpring()
	RunService:BindToRenderStep("ViewmodelMovement", Enum.RenderPriority.Camera.Value + 1, function(dt)
		VmSpring:Update(dt)
		local CF = workspace.CurrentCamera.CFrame * OffsetCFrame
		VmSpring.Target = CF.Position
		Viewmodel:PivotTo(CFrame.new(VmSpring.Position.X, VmSpring.Position.Y, CF.Z))
	end)

Spring module im using

local Iterations = 8

function Spring.newSpring(Mass, Force, Damp, Speed)
	local Self = setmetatable({}, Spring)
	
	Self.Target = Vector3.new()
	Self.Position = Vector3.new()
	Self.Velocity = Vector3.new()
	
	Self.Mass = Mass or 5
	Self.Force = Force or 50
	Self.Damp = Damp or 4
	Self.Speed = Speed or 4
	return Self
end

function Spring:Impulse(ImpulseVector : Vector3)
	local X, Y, Z = ImpulseVector.X, ImpulseVector.Y, ImpulseVector.Z
	
	if not X or X==math.huge or X == -math.huge then X=0 end
	if not Y or Y==math.huge or Y == -math.huge then Y=0 end
	if not Z or Z==math.huge or Z == -math.huge then Z=0 end
	self.Velocity += Vector3.new(X, Y, Z)
end

function Spring:Update(DeltaTime : number)
	local ScaledDeltaTime = (DeltaTime * self.Speed)/Iterations
	
	for i = 1, Iterations do
		local IterationForce = self.Target - self.Position
		local Acceleration = (IterationForce * self.Force) / self.Mass
		
		Acceleration -= self.Velocity * self.Damp
		
		self.Velocity += Acceleration * ScaledDeltaTime
		self.Position += self.Velocity * ScaledDeltaTime
	end
end


Spring.__index = Spring
return Spring

I’m gonna point you to this resource for its viewmodel spring code.

To answer your question though, I think what you want to do is only have the rotation of the viewmodel attached to a spring, which your code does not seem to update; it only springs position.

If you spring the position, it’ll always lag behind the camera, which I’m not sure is what you want. If you’re not going to use the provided resource’s spring module, you’ll have to adapt your current module for CFrame targets.

1 Like

if you are talking about the module
its basically the same im using just a little “condensed” to say

so springs would only be used for the like sway or recoil? no movement or anything if so i wonder how they make their movements look smooth and follow behind a bit when turning or whatever

even if i set the Z to the base CFrame position?

the modules have basically no difference