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 ();
}
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 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.