Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CameraShakerInstance:UpdateShake's timestep #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/CameraShaker/CameraShakeInstance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
--]]


local TICK_RATE = 1/60

local CameraShakeInstance = {}
CameraShakeInstance.__index = CameraShakeInstance
Expand Down Expand Up @@ -48,6 +49,7 @@ function CameraShakeInstance.new(magnitude, roughness, fadeInTime, fadeOutTime)
sustain = (fadeInTime > 0);
currentFadeTime = (fadeInTime > 0 and 0 or 1);
tick = Random.new():NextNumber(-100, 100);
_accumulator = 0,
_camShakeInstance = true;
}, CameraShakeInstance)

Expand All @@ -67,26 +69,32 @@ function CameraShakeInstance:UpdateShake(dt)
NOISE(_tick, _tick) * 0.5
)

if (self.fadeInDuration > 0 and self.sustain) then
if (currentFadeTime < 1) then
currentFadeTime = currentFadeTime + (dt / self.fadeInDuration)
elseif (self.fadeOutDuration > 0) then
self.sustain = false
self._accumulator += dt

while self._accumulator >= TICK_RATE do
self._accumulator -= TICK_RATE

if (self.fadeInDuration > 0 and self.sustain) then
if (currentFadeTime < 1) then
currentFadeTime = currentFadeTime + (TICK_RATE / self.fadeInDuration)
elseif (self.fadeOutDuration > 0) then
self.sustain = false
end
end

if (not self.sustain) then
currentFadeTime = currentFadeTime - (TICK_RATE / self.fadeOutDuration)
end

if (self.sustain) then
self.tick = _tick + (TICK_RATE * self.Roughness * self.roughMod)
else
self.tick = _tick + (TICK_RATE * self.Roughness * self.roughMod * currentFadeTime)
end
end

if (not self.sustain) then
currentFadeTime = currentFadeTime - (dt / self.fadeOutDuration)
end

if (self.sustain) then
self.tick = _tick + (dt * self.Roughness * self.roughMod)
else
self.tick = _tick + (dt * self.Roughness * self.roughMod * currentFadeTime)
end


self.currentFadeTime = currentFadeTime

return offset * self.Magnitude * self.magnMod * currentFadeTime

end
Expand Down