Advertisement
HowToRoblox

SmoothCameraShake

Sep 7th, 2022 (edited)
2,778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local camera = workspace.CurrentCamera
  2. local humanoid = script.Parent:WaitForChild("Humanoid")
  3.  
  4. local start = tick()
  5.  
  6.  
  7. function shakeCamera(initialTrauma, shakeCentre, blur)
  8.     --initialTrauma (necessary, number) = intensity of camera shake
  9.     --shakeCentre (optional, vector3) = position of shake; distance between shake and camera reduces intensity if shakeCentre is given
  10.     --blur (optional, bool) = create blur effect with camera shake
  11.    
  12.     local iterator = 0
  13.     local seed = Random.new():NextNumber()
  14.    
  15.     local blurEffect = nil
  16.     if blur == true then
  17.         blurEffect = Instance.new("BlurEffect")
  18.         blurEffect.Size = 0
  19.         blurEffect.Parent = game.Lighting
  20.     end
  21.    
  22.     while initialTrauma > 0 do
  23.        
  24.         game:GetService("RunService").Heartbeat:Wait()
  25.        
  26.         local now = tick() - start
  27.        
  28.         iterator += 1
  29.        
  30.         local shake = (initialTrauma ^ 2)
  31.        
  32.         if shakeCentre then
  33.             local distance = (camera.CFrame.Position - shakeCentre).Magnitude
  34.            
  35.             shake = shake / math.clamp((distance * 0.1), 0.7, math.huge)
  36.         end
  37.        
  38.         local noiseX = (math.noise(iterator, now, seed)) * shake
  39.         local noiseY = (math.noise(iterator + 1, now, seed)) * shake
  40.         local noiseZ = (math.noise(iterator + 2 + 1, now, seed)) * shake
  41.        
  42.         humanoid.CameraOffset = Vector3.new(noiseX, noiseY, noiseZ)
  43.         camera.CFrame = camera.CFrame * CFrame.Angles(noiseX / 50, noiseY / 50, noiseZ / 50)
  44.        
  45.         if blurEffect then
  46.             blurEffect.Size = shake * 12
  47.         end
  48.        
  49.         local falloffSpeed = 1.6
  50.         initialTrauma = math.clamp(initialTrauma - falloffSpeed * game:GetService("RunService").Heartbeat:Wait(), 0, 1)
  51.     end
  52.  
  53.     humanoid.CameraOffset = Vector3.new(0, 0, 0)
  54.    
  55.     if blurEffect then
  56.         blurEffect:Destroy()
  57.     end
  58. end
  59.  
  60. --example of use:
  61. shakeCamera(1, script.Parent.HumanoidRootPart.Position, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement