0

I am trying to run a DoTween and it runs well but when I want to stop it, it does not stop at all, it keeps running till it finishes. I am using the DoTween Sequence to kill it but somehow it is not working. What is the issue?

public float prevValue = 0;
Sequence mySequence = DOTween.Sequence ();

    private void Update () {
        UpdateSlider ();
        if (Input.GetKeyDown (KeyCode.T)) {
            LerpToThis (90);
        } else if (Input.GetKeyDown (KeyCode.Z)) {
            KillSequence ();

        }
    }

 public void LerpToValue (float newValue) {
        float myValue = prevValue;

            DOTween.To (() => myValue , x => prevValue = x, newValue, 3.0f);

    }

    public void KillSequence () {
        mySequence.Kill ();
    }

Pifo
  • 57
  • 1
  • 8
  • as mentioned by @frankhermes: clean up your code, it seems the `ShouldLerp_` and `DurationFading` are not declared. Preferably add the whole class declarateion and make sure, that your example actually is a piece of code that compiles, so that people can help you. – IARI Mar 21 '21 at 16:27
  • 1
    @IARI thank you, i have cleared it and removed all non-dependencies. – Pifo Mar 22 '21 at 08:45
  • I answered a similar question here in detail: [https://stackoverflow.com/questions/37214649/how-to-kill-current-running-dotween-sequence-in-unity3d](https://stackoverflow.com/questions/37214649/how-to-kill-current-running-dotween-sequence-in-unity3d) – Onat Korucu Nov 10 '21 at 10:45

2 Answers2

3

Apart from the fact that your code uses undeclared variables (like ShouldLerp_) and on pressing 'T' you call a different method than is shown in your excerpt, I'll assume your LerpToThis call also tweens using DOTween.To, but when you try to stop tweening you call mySequence.Kill() even though that sequence isn't the thing that is doing the tweening. So that's why the tween doesn't stop.

Call Kill() on the result of the DOTween.To() call, or give that tween an Id and call DOTween.Kill(id) instead.

frankhermes
  • 4,720
  • 1
  • 22
  • 39
0

frankhermes has said it all, I'm just elaborating the solution a bit:

Within the LerpToValue method, you create a Tween with the line

DOTween.To(() => myValue , x => prevValue = x, newValue, DurationFading);

The method DOTween.To does return an object of type Tween on which you can call Kill, i.e. first you add a declaration to your class

private Tween myTween;

Then you store the tween when you create it in LerpToValue

myTween = DOTween.To (() => myValue , x => prevValue = x, newValue, DurationFading);

And finally, in KillSequence (which you will want to rename, since, as frank mentioned, there is no sequence used) you call

myTween.Kill();

You can get rid of the DOTween.Sequence () entirely.

IARI
  • 1,217
  • 1
  • 18
  • 35