I’m currently having issues with some random spring module and camera.
This spring module works same on different framerate but there’s some problems…
Recoil starts to look strange on 60 FPS, and i don’t know how to fix it.
How it looks on 60 FPS:
How it looks on 240 FPS:
As you see, camera Z rotation is really high on low framerate.
Spring module:
local RunService = game:GetService('RunService')
-- Config
local Iterations = 8
local curr_Delta = 0
-- Module
local SpringModule = {}
-- Functions
function SpringModule.new(self, Mass, Force, Damping, Speed)
local Spring = {
Target = Vector3.new(),
Position = Vector3.new(),
Velocity = Vector3.new(),
Mass = Mass or 5,
Force = Force or 50,
Damping = Damping or 4,
Speed = Speed or 4,
}
function Spring.shove(self, Force)
local X, Y, Z = Force.X, Force.Y, Force.Z
if X ~= X or X == math.huge or X == -math.huge then
X = 0
end
if Y ~= Y or Y == math.huge or Y == -math.huge then
Y = 0
end
if Z ~= Z or Z == math.huge or Z == -math.huge then
Z = 0
end
local EndVector = Vector3.new(X, Y, Z)
self.Velocity = self.Velocity + (curr_Delta * 60) * EndVector
end
function Spring.update(self, Delta)
local ScaledDeltaTime = Delta * self.Speed / Iterations
curr_Delta = Delta
for Index = 1, Iterations do
local IterationForce = self.Target - self.Position
local Acceleration = (IterationForce * self.Force) / self.Mass
Acceleration = Acceleration - self.Velocity * self.Damping
self.Velocity = self.Velocity + Acceleration * ScaledDeltaTime
self.Position = self.Position + self.Velocity * ScaledDeltaTime
end
return self.Position
end
return Spring
end
-- Return
return SpringModule
Update recoil lines:
local updatedRecoil = self.springs.recoil:update(dt)*5
camera.CFrame *= CFrame.new(0, 0, -updatedRecoil.Z*8) * CFrame.Angles(updatedRecoil.X/1.5, -updatedRecoil.Y, -updatedRecoil.Z*4)
Shoving spring:
local newRecoil = Random.new():NextNumber(self.lastObjects.lastRecoil.min, self.lastObjects.lastRecoil.max)
self.springs.recoil.Speed = 22
self.springs.recoil:shove(Vector3.new(newRecoil/75,
Random.new():NextNumber(-3/weaponData.recoil.max, 3/weaponData.recoil.max)/60,
newRecoil/30
)/4)