Camera Shake Based On Distance

Hello!

I have a camera shake code that says when within a certain distance, the camera will shake.

The Task:

I am trying to figure out a way to make the camera shake more violently the closer you are! The problem is, I have no idea what I’m doing :laughing: I would appreciate some help on how I’m supposed to do this.

Here is the code and if you have any questions feel free to ask:

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
	end
	return false
end

while task.wait() do
	if Check() then -- If entity is near it'll mark as true
		local x = math.random(-100,100)/100
		local y = math.random(-100,100)/100
		local z = math.random(-100,100)/100
		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
1 Like

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!

6 Likes

I understand every aspect of this code and I appreciate the comments! Not many people will do what you did by explaining the code and I have to say it is well appreciated! Thank you very much.

4 Likes

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