Increasing a tween value?

In a lot of games when you go to open a egg it gives a shaking effect which gradually speed up and widened the angle before it opens. My question is how do they gradually increase that tween value well increasing the angle?

1 Like

I would assume that something akin to repetitive tweening is done, though I can imagine this is far off the mark and probably wouldn’t accomplish what you need.

do
    local shakeTime = 1
    for i = 1, timesToShake do
        local tween = createTweenWithShakeTimeAsTweenInfoTimeProperty
        tween.Completed:Wait()
        shakeTime = shakeTime - somethingThatIncorporatesTimesToShakeProbably
    end
end

For a start, it could be something, though I don’t really recommend it in the long run since there’s almost undoubtedly a better method floating around out there.

To start, they are likely not using TweenService because tweens are static; they cannot be changed on-the-fly to increase the shaking angle.

Rather, it is a loop repeated for a specific amount of time. As the time continues, the angle of the shake increases.

If we wanted an egg to shake left 45 degrees, then shake to the right 45 degrees, and then return to the center, all in 2 seconds, a simple sine function can be used.


This function could be repeated as many times as you want, but the shake angle will always be 45 degrees.

If we instead want to increase the maximum shake angle over time, we need to vary the sine function with time.

Your code might follow this format:

local ShakeTime = 2 -- how long to shake egg
local t = tick() -- the time at the start of the shake

while tick() - t < ShakeTime do
    local deltaT = tick() - t -- the amount of time that has elapsed since the start of the animation
    local shakeAngle = deltaT * math.sin(5 * math.pi * deltaT) -- similar to the above function
    Egg.CFrame = CFrame.Angles(0, 0, shakeAngle)
end

I’ve glossed over how I arrived at the shakeAngle function, as it requires knowledge of trigonometry. I can explain more if you’re curious, but playing with the numbers yourself is a good place to start.

EDIT: If we play with the function, we can get some really crazy shakes! Have a go at it. I’m using Graphing Calculator to graph these functions.

11 Likes