Camera shake roughness Dependent on distance [EZ Camera Shake] (Solved)

I’m trying to find a way on how to make the camera shaking intensify the closer to a specific object you get. something similar to this, Camera Shaking Static (ROBLOX NEXTBOT) - YouTube

I’ve tried using the players camera and torso as a way to find the distance and use it to calculate the roughness but I don’t know how to make the function update to the new number. Because the way that I’m currently using will just make the camera almost instantly ramp up into insanity. I would appreciate any help :D.

local mod = require(game.ReplicatedStorage.WhiteList.CameraShaker)
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded
local torso = chr:WaitForChild("Torso")
local maxDis = 75

local camshaker = mod.new(Enum.RenderPriority.Camera.Value,function(shakeCf)
	cam.CFrame *= shakeCf
end)

camshaker:Start()

while true do
	local cPos = cam.CFrame.Position
	local tPos = torso.CFrame.Position
	local pos = (tPos - cPos).Magnitude
	if pos < maxDis then
		shakeVal = 200/pos
	end
	print(math.round(shakeVal))
        --I tried to use this to see if it would update just the roughness but it would just create a new shake.
	camshaker:StartShake(1,shakeVal,1)
	wait()
end
1 Like

This is some old code I found from one of my past games using some math I found online.
Does this help?

local Camera = workspace.CurrentCamera

local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
	Camera.CFrame *= shakeCf
end)

if character:FindFirstChild("Head") then
	camShake:Start()
	
	local Dist = (character.Head.Position - Distance).Magnitude
	local Constant = 80
	local Clamp = 2
	local Damping = 3.5
	local DistValue = (Constant/(Dist + Clamp)) ^ (1/Damping)
	local Shake = camShake:Shake(CameraShaker.Presets.Explosion)
	Shake:SetScaleMagnitude(DistValue)
end

Edit: Set the Distance variable to the position of the object

1 Like

I’m not quite sure if I’m supposed to put this script in a loop I’m still pretty new at using this module
but I put it in a while loop and the camera still shook really fast I’ll try changing some values to see if that’s what’s causing it.

1 Like

If you put it in a loop it will keep creating new shakes that overlap.
Just create one and play it alone but if you want it to be consistent then I suggest you keep it in a loop and mess with the Constant, Clamp and Dampening values.
I think lowering the Constant value should decrease the intensity but it if doesn’t then try changing the others.

2 Likes

I accidentally found the solution by using the “Shake:SetScaleMagnitude(DistValue)” from your script and changing it into “Shake:SetScaleRoughness” into my previous script and it surprisingly worked!
(I found the Shake:SetScaleRoughness inside the module script called “CameraShakeInstance” for anyone wondering)

I think you can change the “tpos” and the “cpos” to any object and it would work fine

(Edit) I’ve recently made an edit to the script because I’m currently using it for a project. but I forgot to add the “Shake:SetScaleMagnitude()” Function. The function changes the magnitude of a shake making it either “softer” or a “sharper” shake.

so if I used it so the further you get away the softer the shake gets. because before the edit it would make it so it stays the same the further you are.

for this script specifically I used this method.

Shake:SetScaleMagnitude(math.clamp(10/pos, 0,1))

also I placed it under Shake:SetScaleRoughness(shakeVal) because I thought it was safe there.

your distance divides 10 and caps it out at 1 if the player gets close enough.

ok final thing I’m sorry about the script being ugly.

wait()
local mod = require(game.ReplicatedStorage.WhiteList.CameraShaker)
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded
local maxDis = 75

local inRange = false

local camshaker = mod.new(Enum.RenderPriority.Camera.Value,function(shakeCf)
	cam.CFrame *= shakeCf
end)
local Shake = camshaker:ShakeOnce(1,1,1)
while true do
	local cPos = cam.CFrame.Position -- Can be changed for any Object in the game.
	local tPos = chr:FindFirstChild("Torso").CFrame.Position
	local pos = (tPos - cPos).Magnitude
	if pos < maxDis then
		local shakeVal = 200/pos
		camshaker:Start()
		Shake:SetScaleRoughness(shakeVal)
		Shake:SetScaleMagnitude(math.clamp(10/pos, 0,1))
	else
		camshaker:Stop()
	end
	wait()
end

Thank you for the help because your script helped me find something I didn’t know the module had lol.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.