Possible to transition Tween.Shake to a small/bigger shake with ease smoothly? #56
-
For example when a character is charging up I want it to gradually get shakier. Also when the character stops charging I want it to stop shaking slowly over time with ease like over 1 second and not immediate stop. Currently I'm trying to do this without having a duration so I can dynamically control it with animation events. Both elapsedTime and progress are close to what I want but not quite as it suddenly changes the shake visually then eases to a stop barely. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey, thanks for a great question! Animations with undefined duration is typically not a good fit for a tween library. It is possible to achieve this effect with a tween library, but the code will not look pretty :) Tween shakeStrengthTween;
Tween shakeTween;
float currentShakeStrength;
void Update() {
if (Input.GetMouseButtonDown(0)) {
shakeStrengthTween.Stop();
if (shakeTween.isAlive) {
// Animate shake strength to 0f, then stop the shake
shakeStrengthTween = AnimateShakeStrength(0f)
.OnComplete(this, target => target.shakeTween.Stop());
} else {
// Start shake and animate shake strength to 1f
var shakeSettings = new ShakeSettings(strength: new Vector3(2f, 2f), duration: 10f, frequency: 10f, enableFalloff: false, cycles: -1);
shakeTween = Tween.ShakeCustom(this, Vector3.zero, shakeSettings, (target, shakeVal) => {
target.transform.position = shakeVal * target.currentShakeStrength;
});
shakeStrengthTween = AnimateShakeStrength(1f);
}
}
}
Tween AnimateShakeStrength(float targetStrength) {
var settings = new TweenSettings(duration: 0.5f, ease: Ease.Linear);
return Tween.Custom(this, currentShakeStrength, targetStrength, settings, onValueChange: (target, val) => {
target.currentShakeStrength = val;
});
} |
Beta Was this translation helpful? Give feedback.
-
@doot56 Alternative version with one field less and with public Tween shakeTween;
float currentShakeStrength;
public void AnimateShakeStrength(float targetStrength) {
var settings = new TweenSettings(duration: 0.5f, ease: Ease.Linear);
Tween.Custom(this, currentShakeStrength, targetStrength, settings, onValueChange: (target, newShakeStrength) => {
target.currentShakeStrength = newsShakeStrength;
if (newsShakeStrength == 0f) {
target.shakeTween.Stop();
} else {
target.StartShake();
}
});
}
void StartShake() {
if (!shakeTween.isAlive) {
var shakeSettings = new ShakeSettings(strength: new Vector3(2f, 2f), duration: 10f, frequency: 10f, enableFalloff: false, cycles: -1);
shakeTween = Tween.ShakeCustom(this, Vector3.zero, shakeSettings, (target, shakeVal) => {
target.transform.position = shakeVal * target.currentShakeStrength;
});
}
}
/// Test
void Update() {
if (Input.GetKeyDown(KeyCode.A)) {
AnimateShakeStrength(1f);
}
if (Input.GetKeyDown(KeyCode.D)) {
AnimateShakeStrength(0f);
}
} |
Beta Was this translation helpful? Give feedback.
Hey, thanks for a great question!
Animations with undefined duration is typically not a good fit for a tween library.
It is possible to achieve this effect with a tween library, but the code will not look pretty :)
In this code snippet I use Tween.ShakeCustom() and multiply the shakeVal by a currentShakeStrength. Then I animate currentShakeStrength to fade in/out the shake.