Skip to content

Instantly share code, notes, and snippets.

@boykopb
Created August 16, 2022 06:24
Show Gist options
  • Select an option

  • Save boykopb/a020cbdf61514d92bdb2629ef64dbb53 to your computer and use it in GitHub Desktop.

Select an option

Save boykopb/a020cbdf61514d92bdb2629ef64dbb53 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
/// <summary>
/// Shake gameObject's scale like DoShake in DOTween.
/// </summary>
public class ScaleShaker : MonoBehaviour
{
/*
Paste this in EDITOR to _animationCurve field:
UnityEditor.AnimationCurveWrapperJSON:{"curve":{"serializedVersion":"2","m_Curve":[{"serializedVersion":"3",
"time":0.051374971866607669,"value":0.5017297863960266,"inSlope":0.0,"outSlope":0.0,"tangentMode":0,
"weightedMode":0,"inWeight":0.0,"outWeight":0.0},{"serializedVersion":"3","time":0.12818242609500886,
"value":1.0443166494369507,"inSlope":0.0,"outSlope":0.0,"tangentMode":0,"weightedMode":0,
"inWeight":0.0,"outWeight":0.0},{"serializedVersion":"3","time":0.1953175663948059,"value":0.7285986542701721,
"inSlope":0.0,"outSlope":0.0,"tangentMode":0,"weightedMode":0,"inWeight":0.0,"outWeight":0.0},
{"serializedVersion":"3","time":0.2499302625656128,"value":1.0105841159820557,"inSlope":0.0,"outSlope":0.0,"tangentMode":0,
"weightedMode":0,"inWeight":0.0,"outWeight":0.0},{"serializedVersion":"3","time":0.3162190914154053,"value":0.7753180265426636,
"inSlope":-0.43630123138427737,"outSlope":-0.43630123138427737,"tangentMode":0,"weightedMode":0,"inWeight":0.8284378051757813,
"outWeight":0.0},{"serializedVersion":"3","time":0.3608998656272888,"value":1.000369906425476,"inSlope":0.23225651681423188,
"outSlope":0.23225651681423188,"tangentMode":0,"weightedMode":0,"inWeight":1.0,"outWeight":0.0}],"m_PreInfinity":2,"m_PostInfinity":2,"m_RotationOrder":4}}
*/
[SerializeField] private AnimationCurve _animationCurve;
[SerializeField] private Vector3 _startScale = new Vector3(0.5f, 0.5f, 0.5f);
[SerializeField] private float _lerpRate = 1f;
private bool _isProcessActive;
public void DoShake()
{
if (!_isProcessActive)
StartCoroutine(ChangeScaleRoutine());
}
private IEnumerator ChangeScaleRoutine()
{
_isProcessActive = true;
var endScale = transform.localScale;
for (float t = 0; t < 1f; t += Time.deltaTime / _lerpRate)
{
transform.localScale = Vector3.Lerp(_startScale, endScale, _animationCurve.Evaluate(t));
yield return null;
}
transform.localScale = endScale;
_isProcessActive = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment