I would love to help you with this script and code exactly what you would do, but I think it’s a better learning experience if I told you what you could do. So, so I can satisfy both, I’ll put in the script and the explanation of what you’d want to do to get the results you want.
Explanation
It’s actually simple how this works. So, what you’d want to do is firstly get the distance of the player from the entity. That is so we can calculate how strong the shake should be, and multiply it by how strong we want the shake to be when it’s maxed out.
So yeah, in order to shake the camera based on the distance of anything, we just need to use the distance of that thing and divide it to the maximum distance. Afterwards, we just divide the shake amount (that 100) by the quotient of the distances.
So, for example, lets say I was about 225 studs away from the tornado (that’s just a half of the max distance, 450). I’d get only half of the shake, since 450/225 = 2
and we’re dividing that to the 100.
local maxDistance = 450
local distance = 225
100 / (maxDistance / distance)
-- what it should output: 50
local RunService = game:GetService("RunService")
local deb = false -- Debounce (Switch)
local maxDistance = 450 -- Distance threshold
function Check() -- Returns true if entity is near
if (game.Workspace:FindFirstChild("Tornado").Position - game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude <= maxDistance then
return true, game.Players.LocalPlayer:DistanceFromCharacter(workspace:FindFirstChild("Tornado").Position)
end
return false, nil
end
RunService:BindToRenderStep("ShakeCamera", 201, function(dt) --RenderStepped is a better option for loops
local isNear, distance = Check() -- I decided to return the distance if the entity is near.
-- The distance will be used later in the script.
if isNear then
local x = math.random(-100,100)/ 100 / (maxDistance/distance)
local y = math.random(-100,100)/ 100 / (maxDistance/distance)
local z = math.random(-100,100)/ 100 / (maxDistance/distance)
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(x,y,z)math.random(-1, 1) -- Shakes screen randomly
else
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(0,0,0) -- Resets camera
end
end)
I hope this helps, and good luck on your game!