Search Unity

LeanTween - A tweening engine that is up to 5x faster than competing engines!

Discussion in 'Assets and Asset Store' started by dentedpixel, Dec 3, 2012.

  1. shihab37

    shihab37

    Joined:
    Jul 7, 2015
    Posts:
    28
    Hi, I am using LeanTween in my project (which is my first unity game :)) and it has saved me from a lot of hard work and made animations a lot easier for me. Thank you so much for such an awesome engine.

    I am having just a tiny problem. when I use easing effects, the actual animation (moving or scaling) becomes very fast and most of the time is spent on easing effect.
    e.g. with this code:
    Code (CSharp):
    1. LeanTween.moveY(gameObject, 0.0f, 1.0f).setEase(LeanTweenType.easeOutElastic).setOvershoot(0.3f);
    the object moves to the destination very fast and then spends most of the time oscillating at destination. Same thing happens with scale tweening.

    Is there a way to distribute total tweening time between easing and actual movement? for example, spend 30% of total time on moving and 70% time on easing ... something like that

    Thanks!
     
  2. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Shihab,
    Welcome to Unity, I am glad LeanTween is helping to save you time :)

    easeOutElastic doesn't have any parameters to adjust the specifics of how it acts, but I would recommend that you make a custom AnimationCurve object and pass that in for the ease. That way you can finesse the easing until it is just right. The easiest way is to expose a public variable of type AnimationCurve and that way you can adjust it right in the Unity Editor. I hope that helps...
     

    Attached Files:

    Marble likes this.
  3. shihab37

    shihab37

    Joined:
    Jul 7, 2015
    Posts:
    28
    Hello Dented:pPixel,
    Thanks for your reply.

    Actually before I found LeanTween, I have been creating all my animations using AnimationCurves generating them through code and then creating animation clips to use with Mecanim (I can't trust my aesthetic sense or intuition so I mostly rely on maths :D) but the problem with those is that you have to know your object's start and end positions at design time(unless I am missing something).

    If I pass AnimationCurve to LeanTween's easing function, do I need to know object's start & end positions at design time or I can pass normalized curve?
     
  4. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Haha I hear ya, Math can be so much more trustworthy sometimes.

    Yes, you need to make sure that the curve starts from the zero value and ends at 1 (in one second time). With those settings it adjusts it in LeanTween, no matter what time/length values you give it.
     
    shihab37 likes this.
  5. shihab37

    shihab37

    Joined:
    Jul 7, 2015
    Posts:
    28
    Great .. will give that a try.
    Thanks man!
     
  6. creatify

    creatify

    Joined:
    May 1, 2014
    Posts:
    13
    I'm not sure if this is the right place for this post. I notice that there doesn't appear to be an overload method for LeanTween.cancel(rectTransform:RectTransform).

    How do I cancel a tween on a rect transform and how do I check if a RectTransform isTweening? Thanks!
     
  7. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey there, you have come to the right place to post such things :)

    That is a good point, I did leave that out by mistake. I will add that in tonight. In the meantime you can test against the gameObject that the RectTransform is attached to with the same methods and it should work fine.
     
  8. creatify

    creatify

    Joined:
    May 1, 2014
    Posts:
    13
    Sweet, thank you for the quick response!
     
  9. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Novice question about tweening both x,y,x POSITION AND x,yz ROTATION of a gameObject. What is the best way to do this?Can I combine both in a single tween? Or are these separate tweens on the same gameObject?
     
  10. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Separate tweens. Just start them both simultaneously and they should run in parallel.
     
  11. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Yup, just what Marble said, almost all the tweens can be run on the same object at the same time (although you can't really do like two different types of rotateAround on the same object at the same time). In some situations you have to get a little creative like if you want to move the x position and y position with different tweens or timing you would use a LeanTween.moveX and LeanTween.moveY in parallel.
     
  12. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hiya!

    I think I've found a bug with LeanTween.value<Color> and SpriteRenderers. Basically if you have a LeanTween.value<Color> tween on a sprite, it automatically sets the SpriteRenderer's colour each update, whereas I want to use the updated colour in my callback function.

    Here's a simple script to pop on a sprite to see what I'm talking about:

    Code (CSharp):
    1. public class LeanTweenValueSprite : MonoBehaviour {
    2.     SpriteRenderer spriteRenderer;
    3.     float tweenTime = 1f;
    4.  
    5.     void Start() {
    6.         spriteRenderer = GetComponent<SpriteRenderer>();
    7.         StartTween();
    8.     }
    9.  
    10.     void StartTween() {
    11.         LeanTween.value(gameObject, ColorUpdate, spriteRenderer.color, spriteRenderer.color == Color.white ? Color.black : Color.white, tweenTime).setOnComplete(StartTween);
    12.     }
    13.  
    14.     void ColorUpdate(Color color) { }
    15. }
    Nothing should be changing the sprite's colour, however the sprite does fade between black and white. I was going mad combing through my code for what was wrong! :D
     
    Last edited: Sep 15, 2015
  13. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Heya! That is definitely a bug, I hadn't thought of that use-case before. I can see why that bug would show up. I am working on a bug-fix now, and I will let you know when I have one. In the meantime you can just target your LeanTween.value on a different gameObject that should be a way to get around it... Thanks for letting me know :-D
     
  14. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Hi

    I have a float variable I'm trying to increment from 0 to 1

    is there a way I can do this with LeanTween, I also want to use ease types
    e.g.
    could ease out of 0 and into 1
     
  15. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Excellent, thanks! Yeah I've already moved the tween onto a different gameObject, definitely works as a fix!

    Also I'm curious about why LTDescr has a .cancel() method, when it is seemingly unreliable? I read in the documentation that LeanTween.cancel(gameObject, id) is the best way to cancel a tween, and this works every time. But LTDescr.cancel() existing made me sink a bit of time into it wondering what was wrong with my setup. Is there a reason you've left it if it doesn't reliably cancel the tween? Or was I perhaps using it wrong?
     
  16. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Yeah I have started the process of re-arranging the documentation so as not to highlight the LTDescr .cancel() method . Since it can cause issues if not used correctly (which is easy to do). Sorry it made you lose a bunch of time! I think I may make it deprecated for the next release (and then maybe remove it all together for a future release)
     
  17. AbgaryanFX

    AbgaryanFX

    Joined:
    Jan 9, 2010
    Posts:
    167
    Hi dentedpixel !
    How can I reverse the animation ?

    Code (CSharp):
    1. void AnimFinger()
    2.     {
    3.         float t = currentController.lineParts[0].Positions.Length * 0.05f;
    4.         LTDescr ll = LeanTween.move(Mat, ltSpine, t).setEase(LeanTweenType.easeInOutSine);
    5.         ll.direction = dir ? 1f : -1f;
    6.         dir = !dir;
    7.         ll.onComplete = () =>
    8.         {
    9.             LeanTween.alpha(Mat, 0f, .5f);
    10.             LeanTween.delayedCall(Mat, 1f, () =>
    11.             {
    12.                 LeanTween.alpha(Mat, 1f, .2f);
    13.                 AnimFinger();
    14.             });
    15.         };
    16.         //dir = !dir;
    17.      
    18.         print(ll.direction);
    19.     }
     
  18. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey there,
    You can either set the tween to pingPong if you want it to reverse right away, but I expect that's not what you are looking for.

    You can call reverse a spline by passing in the spline like this:

    LeanTween.move(Mat, Array.Reverse( ltSpine.pts ), t).setEase(LeanTweenType.easeInOutSine);

    Array.Reverse info. I haven't tested it but I think that should work.

    I hope that helps!
    Russ
     
  19. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Not at all, LeanTween has saved me much, much more time than I've lost investigating little issues! :)
     
  20. emde

    emde

    Joined:
    Aug 1, 2015
    Posts:
    6
    I just started using LeanTween and I must say it works great! In working with it however, I’ve run into a snag and I can’t figure out if LeanTween is capable of doing it. Any pointers would be appreciated. I simply need to modify an existing tween, an object moving across the screen using .moveX. I need to change the speed by a factor of 1.5x when a user taps a “boost” button. I’ve tried modifying the time value using LeanTween.description(tweenID).time, however this simply jumps back and forth between where the object WOULD have been if the time was faster from the beginning. As soon as the button is released the object jumps back.

    Maybe I should just use unitys built in physics and rely on velocity modifications? I was hoping there would be an easy way to do this with LeanTween. Thoughts?

    Thanks!
     
  21. Bracer

    Bracer

    Joined:
    Sep 4, 2015
    Posts:
    19
    Hi, I am unable to see the "LeanTweenPath" option under Create, how do I enable that option in the IDE so that I can select it ? I have already imported LeanTween :)
    Thanks.
     
  22. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    dentedpixel

    There's something weird happening with "setIgnoreTimeScale"
    I got a Tween script attached to 2 cubes, one is set to ignore TimeScale and the other isn't

    the "IgnoreTimeScale" part is working, but the cube with "IgnoreTimeScale" set to true is jumping ahead on start, so it's getting to the end of the Tween faster than the other

    What am I missing?

    my code
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class IgnoreTimeScaleTest01 : MonoBehaviour
    6. {
    7.  
    8.     public LeanTweenType easeType;
    9.     public float time;
    10.     public bool boolean;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         LeanTween.moveX( gameObject, 10f, time).setEase(easeType).setIgnoreTimeScale(boolean);
    16.     }
    17.  
    18. }
    19.  
    @Bracer
    Sorry, by posting my question, I'm putting you question under the stack
     
  23. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi Bracer,
    Creating a path with the GUI only works if you have downloaded the LeanTween Editor. If you do not have the editor you can still make paths manually though, it just can be a little more time consuming. You can find the LeanTween Editor here.

    I hope that helps,
    Russ
     
  24. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey EmDe, sorry about the late response, I thought I replied before I left on vacation but it seems my post never got through (hence the over one week delay).

    That is a tough one, there is a dtManual variable that you can set which will boost up all tweens set to use it like:

    LeanTween.moveX(gameObject, 1f, 5f).setUseManualTime( true );

    // Later
    if(boost)
    LeanTween.dtManual = 3f;

    That will boost all tweens set to use it though, not the individual one (which I assume is the behaviour you are looking for?). It wouldn't be hard to make this property a per-tween dtManual... I don't think anyone is actually using it so it wouldn't be a big deal to change :)
     
  25. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Gravitron,
    Yeah that is sort of a quirk of the Start function, by the time it actually hits the Start code often up to a second of time has passed, the variable that ignoreTimeScale tweens use, does not take into account this delay of the Start code which can cause the behavior you see :-/. There is a pretty easy way around this though, if you just add a minuscule delay to the start of you ignoreTimeScale tween like:

    Code (CSharp):
    1. LeanTween.delayedCall(gameObject, 0.01f, ()=>{
    2.             LeanTween.moveX(gameObject, 5f, 1f).setIgnoreTimeScale( true ).setOnComplete( ()=>{
    3.                 Debug.Log("ignore finished Time.time:"+Time.time);
    4.             });
    5.         });
    That should have it complete at mostly the same time as the non-ignore timescale tween.

    Come to think of it, I might be able to fix this in the LeanTween plugin itself, but hopefully this will give you a work-around for the moment...
     
  26. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hey Gravitron,
    I actually made a fix on the LeanTween side that should avoid this issue, if you want to try it out it is on Github .
     
  27. Krunchisoft

    Krunchisoft

    Joined:
    Jul 14, 2014
    Posts:
    22
    Hi Dented Pixel,

    I have a little problem with "rotate by",hope you can help.
    so i wanted to rotate my character by 90 degrees at Y axis everytime i hit left or right arrow button.
    the problem is it's not always stop at exact value like 0,90,180 degrees, it often reach 7.62 , 179, or 1 degree.
    sometimes the tween stop at 45 degrees.
    so how make the value precised?
    thx
     
  28. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Do you allow the Leen to complete one rotation before starting the next? If not that'll produce non deterministic results.

    I've never had such an issue myself, perhaps try something like:

    Always call LeanTween.cancel(gameObject, true) (the true = call onComplete when cancelling) and do something like:

    Code (csharp):
    1. LeanTween.rotate(gameObject, destinationAngle, timeToTween).setOnComplete(delegate()
    2. {
    3.   // you'll need to change this to use euler angle - that's just pseudo code, so amend as needed
    4.   gameObject.transform.rotation = destiationAngle;  
    5. });
    You may also want to add the delta of the destinationAngle from the previous rotation to the next rotation, all depending on what you're wanting.
     
  29. Krunchisoft

    Krunchisoft

    Joined:
    Jul 14, 2014
    Posts:
    22
    Hi Larku,
    i use playmaker.
    the state looks like this.

    Screen Shot 2015-10-18 at 6.53.36 PM.png
     
  30. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Ah, I've never used playmaker so I'm out of my depth here.
     
  31. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    @dentedpixel

    are you putting the "setIgnoreTimeScale" fix in an update on the asset store or should I just get it from Github?
     
  32. Krunchisoft

    Krunchisoft

    Joined:
    Jul 14, 2014
    Posts:
    22
    Hi Larku,
    it turns out that "rotate around by" is more precise and for safety i snap the value at the end.and off course always cancel the tween.
    thx for giving me the idea : D
     
    larku likes this.
  33. timfreeman

    timfreeman

    Joined:
    Oct 21, 2015
    Posts:
    6
    hi @dentedpixel
    first of all, thank you for really great library.

    there's a strange issue on Android (iOS works fine). when you use tween on an object for the first time with a specific easing curve it lags. animation consists of around 3-4 frames. other animations on a same object (or on another object with the same easing) work smooth.

    i found out that you can prevent this "first time lag" by something like this. but im not very comfortable with writing this thing for every animation.
    Code (CSharp):
    1. public void testMove ()
    2. {
    3.     LTDescr desc = LeanTween.move(moveTransform, new Vector3(moveTransform.anchoredPosition.x, (testAnimateStart) ? 105.0f : 55.0f, 0.0f), 0.5f);
    4. #if UNITY_ANDROID
    5.     desc.setDelay(0.03f);
    6. #endif
    7.     desc.setOnComplete(() => {Debug.Log("complete"); testAnimateStart = !testAnimateStart;});
    8. }
    am i missing something ? is there a way to "preload tween" somehow (tried LeanTween.init, doesn't help) ?
     
  34. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi TimFreeman,
    That is strange, is the first tween fired right on the Start method of the scene? There was an issue on a tween having bad timing when fired right of the Start method unless you put a small delay, but I fixed it in the latest on Github. If you want to check out that version to see if it gets rid of your problem that would help.

    Thanks, I am glad you like the engine. Sorry it took so long to respond! My notifications seem to be broken for this forum :-/
     
  35. timfreeman

    timfreeman

    Joined:
    Oct 21, 2015
    Posts:
    6
    hi

    my response took a little time too, so not a problem = )

    no, its not getting called from Start method. its from Button OnClick event.
    will try latest version from github and post a bit later
     
    Last edited: Nov 3, 2015
  36. timfreeman

    timfreeman

    Joined:
    Oct 21, 2015
    Posts:
    6
    this "initial lag" problem persists in latest version (2.30) on Android.
    still works fine on iOS.
     
  37. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hmmm that is bizarre, Is there anything else intense happening on that button click event? Not much happens when you initially call move on a tween, so I can't see why this would cause any overhead...

    I think I have an old Kindle Fire tablet around that I can test on to see if I can reproduce it.
     
  38. timfreeman

    timfreeman

    Joined:
    Oct 21, 2015
    Posts:
    6
    there isn't much happening and scene is pretty empty.
    also i looked through LeanTween class code. didn't find anything that may cause this too.

    device im testing on is Samsung Galaxy S3 (GT I9300)
    Unity 5.2.2f1
     
  39. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    @dentedpixel

    is there a way to call multiple methods when a tween finishes tweening using the "setOnComplete" LTDescr

    e.g. setOnComplete (Method1, Method2)

    it seems like something that should be possible

    also I tried the fix you put on Github, still having the same jump in the tween, maybe I'm not getting all the relevant scripts (new to updating packages using Github)
     
  40. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422

    Yeah sure, just do:

    Code (csharp):
    1. setOnComplete(delegate()
    2. {
    3.   Method1();
    4.   Method2();
    5. });
     
    dentedpixel likes this.
  41. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hmmm, that's too bad that didn't fix it. You should just have to update the LeanTween.cs file.
     
  42. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Sorry for such a basic question, but I'm having trouble figuring out how to infinitely rotate a transform at different speeds on each axis. I'm new to this whole tweening thing.

    right now I just have this in update but it doesn't look smooth enough
    Code (CSharp):
    1. obj.transform.Rotate(100*Time.deltaTime,120*Time.deltaTime,40*Time.deltaTime);
     
    Last edited: Nov 7, 2015
  43. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    No worries, can I ask why you are trying to rotate on all the axises at once? Usually you just want to rotate around a specific axis, in LeanTween you can do this like:

    LeanTween.rotateAround ( gameObject, Vector3.left, 90f, 1f );

    // Infinitely do this by

    LeanTween.rotateAround ( gameObject, Vector3.left, 360f, 1f ).setRepeat(-1); // -1 means it repeats infinitely
     
  44. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Thanks! The total effect is rotating around a single arbitrary axis I suppose, but I don't math good enough to find it.
     
  45. linhdinh

    linhdinh

    Joined:
    Mar 16, 2015
    Posts:
    4
    I observe that LeanTween adds another 5MB to final build if I set IL2CPP and universal architecture. Does anyone knows what part of LeanTween causes it?
     
  46. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi LinkDinh,
    Selecting the Universal Architecture means it is making a build that will run on 32-bit and 64-bit systems. I believe behind the scenes it makes two different code-bases for the two architectures, so you can imagine why it would add some extra bloat. I don't think anything in LeanTween can be done to reduce this size, it's just a result of how the build process works. This may be able to be resolved in Apple's new "App Slimming" features in iOS 9 though.
     
  47. bzAdam

    bzAdam

    Joined:
    Aug 4, 2015
    Posts:
    39
    Hi is there a way to cancel delayed tweens without a gameobject?

    I have a class that does not have any gameobjects and am using delayed call to call a delayed method:

    Code (csharp):
    1.  
    2. LeanTween.delayedCall( 0.2f, () => {
    3.                     DoSomething1();
    4.                     DoSomething2();
    5.                 } );
    Thanks.
     
  48. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I've never tried this, but it looks like this may be a way:

    Code (csharp):
    1. LTDescr descriptor = LeanTween.delayedCall( 0.2f, () => {
    2.                     DoSomething1();
    3.                     DoSomething2();
    4.                 } );
    5.  
    6. ...
    7. ...
    8. ...
    9.  
    10. LeanTween.cancel(descriptor.uniqueId);
     
    bzAdam likes this.
  49. bzAdam

    bzAdam

    Joined:
    Aug 4, 2015
    Posts:
    39
    Looks good, thanks!
     
  50. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi bzAdam,
    Yup that's definitely possible, you would do that like:
    int tweenId = LeanTween.delayedCall( 0.2f, () => {
    DoSomething1();
    DoSomething2();
    });

    // Later you can cancel
    LeanTween.cancel( tweenId );
    That should work 99% of the time, but you can have issues canceling a tween you don't expect (if the LTDescr has already completed it can be recycled and used for a new tween).

    It's actually safest just caching off the id to then cancel it later like:
    1. int tweenId = LeanTween.delayedCall( 0.2f, () => {
    2. DoSomething1();
    3. DoSomething2();
    4. } ).id;
    5. ...
    6. LeanTween.cancel( tweenId );