So recently, I’ve tried adding a smooth damp function from unity into roblox. The original source of this method can be found on their github for the following methods I’ve tried porting over:
SmoothDampAngle()
SmoothDamp()
DeltaAngle()
Just to be clear, these are all from the mathf class which is responsible for floats and integers, not vectors.
I also had some help from Sleitnick’s github to refine some of my code. More specifically, SmoothDamp.lua to make sure that they work on my game, so in essence, my code is very similar to theirs except his methods are used to dampen vectors, mine are used to dampen floats. The following is the module i’ve made:
local SmoothDamp = {}
SmoothDamp.__index = SmoothDamp
function SmoothDamp.new()
return setmetatable(
{
MaxSpeed = math.huge;
_update = time();
_velocity = 0
}, SmoothDamp
)
end
local function DeltaAngle(current, target)
local delta = ((target - current) % 6.2831853072)
if (delta > 6.2831853072) then
delta -= 6.2831853072
end
return delta
end
function SmoothDamp:SmoothDampF(current, target, smoothTime)
local currentVelocity = self._velocity
local now = time()
local deltaTime = (now - self._update)
smoothTime = math.max(0.0001, smoothTime)
local omega = 2 / smoothTime
local x = (omega * deltaTime)
local exp = (1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x))
local change = current - target
local originalTo = target
local maxChange = self.MaxSpeed * smoothTime
local change = math.clamp(change, -maxChange, maxChange)
target = current - change
local temp = ((currentVelocity + omega * change) * deltaTime)
currentVelocity = ((currentVelocity - omega * temp) * exp)
local output = (target + (change + temp) * exp)
if ((originalTo - current) > 0.0) == (output > originalTo) then
output = originalTo
currentVelocity = (output - originalTo) / deltaTime
end
self._velocity = currentVelocity
self._update = now
return output
end
function SmoothDamp:SmoothDampAngleF(current, target, smoothTime)
return self:SmoothDampF(current, (current + DeltaAngle(current, target)), smoothTime)
end
return SmoothDamp
and this is the usage:
local smoothDampMod = require(game.ReplicatedStorage.SmoothDampAlgorithm)
smooth = smoothDampMod.new()
task.wait(10)
while wait() do
script.Parent.CFrame = CFrame.new(smooth:SmoothDampF(script.Parent.CFrame.Position.X, script.Parent.CFrame.Position.X + 20, 3), 0, 0)
end
This script is attached to a part.
And this… is the result…
This may literally just be a case of me forgetting something but I have no clue what I’ve actually done wrong. I think I’ve managed to trace the problem down to the SmoothDampF method but I’m still at a complete loss. Can I get some help?