Local Script piece attatching weapon:
local nine = 9
local thirty = 30
local CamZRef = game.workspace.CurrentCamera
local pitchAngleRef = CamZRef.CFrame.LookVector.Y
local UpDoMovRef = CFrame.Angles(pitchAngleRef, 0, 0) * gunCfOffsetVal.Value * gunShotVal.Value * gunLoadVal.Value * CFrame.Angles(0, math.rad(90), 0)
game.ReplicatedStorage.HoldGun:FireServer(UpDoMovRef)
RunService.RenderStepped:Connect(function(dt)
aimPosSpring:update(dt)
aimRotSpring:update(dt)
local CamZ = game.workspace.CurrentCamera
local headCf = Head.CFrame
local BodyAttCf = ToolBase.CFrame
local camCf = CamZ.CFrame
local camRotCf = camCf - camCf.Position
local targBodyAttCf = camRotCf * gunCfOffsetVal.Value
local headCfRot = charac.Head.CFrame - charac.Head.Position
local xRot, yRot, zRot = (camCf * CFrame.Angles(0, math.rad(90), 0)):ToEulerAnglesYXZ()
aimPosSpring.Target = targBodyAttCf.Position
aimRotSpring.Target = Vector3.new(xRot, yRot, zRot)
local rotSpringPos = aimRotSpring.Position
print(xRot, yRot, zRot)
charac.Head.ToolGrip.C0 = headCfRot:Inverse() * CFrame.new(aimPosSpring.Position) * CFrame.Angles(0, math.atan2(camCf.LookVector.X, camCf.LookVector.Z) - math.rad(90), 0) * CFrame.Angles(rotSpringPos.X, 0, rotSpringPos.Z)
end)
Spring Module Script in Local Script:
-- Constants
local ITERATIONS = 8
-- Module
local SPRING = {}
-- Functions
function SPRING.create(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
self.Velocity = self.Velocity + Vector3.new(x, y, z)
end
function spring.update(self, dt)
local scaledDeltaTime = math.min(dt,1) * self.Speed / ITERATIONS
for i = 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 SPRING