This topic seems a little advanced, including for me. In a nutshell, I am making a camera shake that changes the matrix of the camera depending on the distance between the camera and a part. I’m not sure whether I change the math itself or another way to fix the issue.
The first issue I want to resolve is, I want the camera’s shake frequency/sine wave to transition from being normal to shaky depending on the distance. If the player is near out of range, the shake would be less intense, and vice versa if getting closer.
I am trying to remake this camera effect by UglyBurger0 but make it distance based. (If this helps, 0:15 timestamp)
local function Shake(strength, freq, dt)
local R00_EQUATION = (math.abs(math.sin(tick() * (freq/strength) / dt)) + 4.5) / 5.5
local MATRIX = CFrame.fromMatrix(
Vector3.new(0, 0, 0), -- X, Y, Z
Vector3.new(R00_EQUATION, 0, 0), -- r00, r10, r20
Vector3.new(0, 1, 0), -- r01, r11, r21
Vector3.new(0, 0, 1) -- r02, r12, r22
)
return MATRIX
end
local alpha = 1 - (dist / 50)
alpha = math.clamp(alpha, 0, 1)
local strength = lerp(0, 1.25, alpha)
local freq = lerp(1, math.clamp(5 + (10 - 5)*(dist/50), 1, 10), .3)
Camera.CFrame = Camera.CFrame * Shake(strength, freq, dt)
end
end)
If you found my explanation confusing, don’t be afraid to tell me what to re-explain.