Facebook
From bblessed, 1 Year ago, written in C++.
Embed
Download Paste or View Raw
Hits: 198
  1. IEnumerator WiggleIntoPlace(Vector3 startingPos, Vector3 endingPos)
  2. {
  3.     float duration = 0.3f;
  4.     int wiggles = 3;
  5.     float damping = 3f; //> 1
  6.     float wiggle_duration = duration / (wiggles + 1);
  7.     bool easein = false;
  8.  
  9.     for (int i = 0; i <= wiggles; i++)
  10.     {
  11.         float elapsedTime = 0f;
  12.         Vector3 tempPos = endingPos - (startingPos - endingPos) / damping;
  13.         while (elapsedTime < wiggle_duration)
  14.         {
  15.             float t = elapsedTime / wiggle_duration;
  16.             t = t * t * t * (t * (6f * t - 15f) + 10f); //t = t * t * (3f - 2f * t);
  17.             if (i == 0 && easein) { t = Mathf.Sin(t * Mathf.PI * 0.5f); }
  18.             if(i == wiggles) { tempPos = endingPos; }
  19.             float x = startingPos.x - (startingPos.x - tempPos.x) * t;
  20.             float y = startingPos.y - (startingPos.y - tempPos.y) * t;
  21.             float z = startingPos.z - (startingPos.z - tempPos.z) * t;
  22.             transform.localPosition = new Vector3(x, y, z);
  23.             elapsedTime += Time.deltaTime;
  24.             yield return new WaitForEndOfFrame();
  25.         }
  26.         startingPos = tempPos;
  27.     }
  28.     transform.localPosition = endingPos;
  29. }