0

I have a shake tweener initilialized like this:

mainCamera.DOShakePosition (100f, 0.05f, shakeVibrato, 90, false, false);

The duration 100 is just put there so I can have some big value. The idea is to change the vibrato/strength of the shake while it is active.

Imagine some vibration source is coming closer to the player. As it approaches, the vibrato increases, as it passes by the vibrato decreases. How can I manipulate these values while tweener is active? I saw some ChangeValues() methods but I'm not sure what they do and the documentation is not very clear about them.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Martin Asenov
  • 1,288
  • 2
  • 20
  • 38

1 Answers1

1

As far as I know, there's no proper way to do this for DOTween's DOShake methods (though I would love to be corrected). One really hacky way to do this is by setting the duration to a low-ish value, and re-initializing the tween with different values in it's OnComplete callback. It's pretty far from ideal, since you're not changing the values during the tween, but rather at an interval - resulting in a stepped or sudden change. Resource-wise, I don't imagine this being very efficient either.

Tweener shakeTween;
TweenCallback shakeTweenComplete;

void Start()
{
    shakeTweenComplete = () =>
    {
        shakeTween = transform.DOShakeRotation(0.1f, strength: shakeStrength, vibrato: shakeVibrato, fadeOut: false).SetRelative();
        shakeTween.OnComplete(shakeTweenComplete);
    };

    // Invoke the callback instead of having duplicated code
    shakeTweenComplete.Invoke()
}

I have shakeVibrato modified elsewhere. I also tried to do this with SetLoop and OnStepComplete, with no luck.

EDIT - For future reference, this is my take on how OP ended up solving this issue. I substituted changing the vibrato with Tweener.timeScale, since it ends up looking the same anyway.

public float shakeMultiplier = 1.0f;
public float shakeTimeScale = 1.0f;

// These values won't be changed
public float baseShakeDuration = 1.0f;
public float baseShakeStrength = 0.1f;
public int baseShakeVibrato = 10;

Vector3 shakePosition;
Tweener shakeTween;

void Start()
{
    shakeTween = DOTween.Shake(() => shakePosition, x => shakePosition = x, baseShakeDuration, baseShakeStrength, baseShakeVibrato, fadeOut: true)
           .SetLoops(-1, LoopType.Restart);
}

void Update()
{
    transform.localPosition = shakePosition * shakeMultiplier;
    shakeTween.timeScale = shakeTimeScale;
}
Macklin
  • 146
  • 1
  • 1
  • 8
  • 1
    actually I used the getter/setter method to populate a local variable with the shake local position. Then I had a second variable that held the proximity to the "shake source" object. Then I multiplied the first with the second to update the object's local position in an Update() method. That is how I got around it – Martin Asenov Jan 24 '18 at 10:56