Search Unity

How to Pause & Resume a Lerp

Discussion in 'Scripting' started by ClaudiaKrog, Jul 22, 2020.

  1. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    Hello,

    I have a function where I lerp a sphere up in scale, then I lerp back down in scale. This happens in the update function.

    I also have a way to pause the lerp, and then resume lerping. However, once I resume lerping, the scale starts from where it should be if it had not been paused, rather than starting from the paused scale.

    Any advice is appreciated. Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    We won't be able to help you without seeing your code (in code tags, please).

    But "pausing and resuming a Lerp" doesn't make a lot of sense. Lerp isn't a movement or animation; you put in two things, give it a value from 0-1, and it find a value in between those two things - that's it. A frequent usage of Lerp includes passing in some sort of time variable, and that makes it a piece of animation. Understanding that there's no movement inherent in a Lerp is probably vital to fixing your problem.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I suggest you look into a tweening system, such as leantween or dotween. While you could just write the system yourself, I know leantween has a built in pause and it wouldn't surprise me if dotween did also.
     
  4. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    Thanks! I will look into both of those and see if that helps
     
  5. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    I tried adding an animation component to the objects that I would like to move. There is an easy Play() and Stop() method I can call, which is great. However, I seem to have the same issue: when I stop an animation, and then replay it, the animation starts over from the beginning, rather than resuming from where the animation left off.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In that case you should be able to use Pause()
     
  7. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    It seems there is no pause() ability. However, I've found this code:

    foreach (AnimationState state in myAnimation)
    {
    state.speed = 0f;
    }

    Which did the trick! Strangely, enabling and disabling also had the similar problem of restarting the anim back at its beginning, rather than resuming.

    However, setting the state speed worked well. Thanks for the thoughts!