Patrick Klug
Patrick Klug

Reputation: 14411

How do you animate Unity Time.timeScale using an easing function with DOTween?

I'm trying to animate Time.timescale using an easing function with DOTween.

Tweening the value itself seems to work:

DG.Tweening.DOTween.To(value => Time.timeScale = value, 1, 0, 0.4f);

But all the SetX() methods mentioned in the documentation are not available on the object this returns and while I found out that I could set the timeScale property, there doesn't seem to be any way to use an easing function for this tween.

What am I missing?

Upvotes: 4

Views: 10583

Answers (2)

Patrick Klug
Patrick Klug

Reputation: 14411

Figured it out:

Unfortunately it seems that all SetX() methods in DOTween are implemented as extension methods. This means that these methods are simply not available if you are trying to use DOTween by specifying the full type, rather than including the namespace.

This works:

using DG.Tweening;
///
DOTween.To(()=> Time.timeScale, x=> Time.timeScale = x, 2, 0.4f).SetEase(Ease.InQuad).SetUpdate(true);

This does not work:

DG.Tweening.DOTween.To(()=> Time.timeScale, x=> Time.timeScale = x, 2, 0.4f).SetEase(DG.Tweening.Ease.InQuad).SetUpdate(true);

Upvotes: 6

Jayson Ash
Jayson Ash

Reputation: 709

It looks to me like you're wanting to pause execution by slowing time down gradually rather than instantaneously stopping it and you want to use an easing method.

To set an ease on a DOTween just add on the Ease method call via chaining like this:

DG.Tweening.DOTween.To(value => Time.timeScale = value, 1, 0, 0.4f).SetEase(Ease.InCubic));

Chain more methods together like this, this runs Debug.Log on every Update call:

DG.Tweening.DOTween.To(value => Time.timeScale = value, 1, 0, 0.4f).OnUpdate(()=>{ Debug.Log(Time.timeScale); }).SetEase(Ease.InCubic);

Running this code shows that Time.timeScale doesn't actually get to zero, probably due to floating point precision errors; however, using the OnUpdate chain method Time.timeScale can be assigned zero once it gets close enough:

DG.Tweening.DOTween.To(value => Time.timeScale = value, 1, 0, 0.4f).OnUpdate(()=>{ if (Time.timeScale < 0.001f) Time.timeScale = 0; }).SetEase(Ease.InCubic);

I hope this helps!

Upvotes: 1

Related Questions