kyo_kyo

camera shake

May 7th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. local module = {}
  2. local active = {}
  3. local connections = {}
  4. local runs = game:GetService("RunService")
  5.  
  6. local plr = game.Players.LocalPlayer
  7. local hum = plr.Character and plr.Character:FindFirstChildOfClass("Humanoid")
  8.  
  9. local function calc(t, i)
  10. local seed = math.random(1, 100000)
  11. local noise = 0.7
  12.  
  13. local x = (math.noise(seed, t, 0) * noise + math.random(-1, 1) * (1 - noise))
  14. local y = (math.noise(seed + 1, t, 0) * noise + math.random(-1, 1) * (1 - noise))
  15. local z = (math.noise(seed + 2, t, 0) * noise + math.random(-1, 1) * (1 - noise))
  16.  
  17. return Vector3.new(x, y, z) * i
  18. end
  19.  
  20. local function dist(cam, part)
  21. local dist = (part.Position - cam.CFrame.Position).Magnitude
  22. local max = 75
  23. local min = 5
  24.  
  25. return math.clamp(1 - (dist - min)/(max - min), 0, 1)
  26. end
  27.  
  28. local function update(dt)
  29. local cam = workspace.CurrentCamera
  30. if not cam then return end
  31.  
  32. if not next(active) then
  33. if hum then
  34. hum.CameraOffset = Vector3.zero
  35. end
  36. if connections.render then
  37. connections.render:Disconnect()
  38. connections.render = nil
  39. end
  40. return
  41. end
  42.  
  43. local tot = Vector3.zero
  44.  
  45. for part, data in pairs(active) do
  46. if part and part.Parent then
  47. data.e = (data.e or 0) + dt
  48. if data.d and data.e >= data.d then
  49. active[part] = nil
  50. else
  51. local factor = dist(cam, part)
  52. if factor > 0 then
  53. local i = data.i * factor
  54. local offset = calc(data.e, i)
  55. tot += offset
  56. end
  57. end
  58. else
  59. active[part] = nil
  60. end
  61. end
  62.  
  63. if hum then
  64. hum.CameraOffset = tot
  65. end
  66. end
  67.  
  68. function module.start(part, d, i)
  69. if not part or not part:IsA("BasePart") then
  70. warn("Shake target must be a BasePart")
  71. return
  72. end
  73.  
  74. active[part] = {
  75. i = i ~= nil and math.clamp(i, 0.025, 10) or 0.75,
  76. d = d ~= nil and d or math.huge,
  77. e = 0
  78. }
  79.  
  80. if not connections.render then
  81. connections.render = runs.RenderStepped:Connect(update)
  82. end
  83. end
  84.  
  85. function module.modify(part, i)
  86. if active[part] then
  87. if i then active[part].i = math.clamp(i, 0.025, 10) end
  88. end
  89. end
  90.  
  91. function module.stop(part)
  92. active[part] = nil
  93. end
  94.  
  95. local function cleanup()
  96. table.clear(active)
  97. if connections.render then
  98. connections.render:Disconnect()
  99. connections.render = nil
  100. end
  101. end
  102.  
  103. plr.CharacterRemoving:Connect(cleanup)
  104.  
  105. return module
Advertisement
Add Comment
Please, Sign In to add comment