Tweener Part 2: Extensions, First Use Cases, Repeating a Tween

Domen Koneski
3 min readMar 17, 2018

--

Since we have our tweening engine up and running it is time to create tweening first functions. We will start with Transform translations, scaling and rotations.

Lets create a new class that will hold Transform related tweening functions. First one will be called TweenPosition which, as name suggest, will move our Transfrom from one position to another. This function implements our FloatTween.CreateTween() function, it allows to tween either local Transform’s position or world one and last but not least it can hold OnEnd action for your scripting needs (e.g. you want to trigger something when Transform ended moving). Function looks like this:

We can easily call this function from anywhere by simply calling TransformTween.TweenPosition(Transform, endPosition, duration);

Example with stacking two tweens (second one gets called on OnEnd action).

TweenPosition in action!

Basic animations in game development are usually divided as being one-shot (gun shoots) or repeating (rotating propeller). Repeating a tween is also what we would want from this engine to create never ending animations with one-liners. Lets implement repeating tweens.

Add two new methods/functions to our Tween class:

Repeat(count) will set internal counter on how many repeats are left (tween with no repeats has this value set to 0, tween that repeats forever should have this number set to -1). The other method is setting up custom action when repat occur — so we can actually repeat the code. We will use Method chaining for ease of use.

The last thing is adding these rules to our Tween engine which will decrease RepeatsLeft variable and reset DurationLeft to the initial duration. It will also invoke the OnRepeat action:

With our first TransformTween implementation and repeating system we can now do some very simple animations with one liners, but first lets define SwingTransform function.

Calling TransformTween.SwingTransform(transform, 2, 2, -1).Start(); on any transform will produce this repeating animation:

Repeating tween.

Class extensions are really usefull when you want to keep your code clean and tidy. I have written a couple of extension methods for our tweening engine and Transforms. Here is an example of one of the extention, it really looks like we are overriding a method:

This new approach now requires you to import our tweener engine with including our namespace at the top of your script/component, add this: using Koneski.Tweener; You are now ready to use our extensions by simply calling e.g. transform.TweenPosition();

The last example uses randomly selected parameters for our swinging tween by using extension methods:

Swings and more swings!

Next up we will take a look how to sequence our tweens to create complex animations and easing methods (so we don’t only use linear interpolation).

You can find the source code on my Github page: https://github.com/domenkoneski/simple-tweener-unity3d

Until next time!
Domen

--

--

Domen Koneski

Games are not fun. They can create an experience and teach you a life-long lesson.