Skip to content

Instantly share code, notes, and snippets.

@robert-michels
Last active March 23, 2021 02:03
Show Gist options
  • Select an option

  • Save robert-michels/13bb15385194385ba5d5700ee224d858 to your computer and use it in GitHub Desktop.

Select an option

Save robert-michels/13bb15385194385ba5d5700ee224d858 to your computer and use it in GitHub Desktop.
// CODE SNIPPET, NOT THE FULL FILE //
//Coroutine that controls growth / death animation, colliders, presence and perch targets
IEnumerator TreeLerpSize (float time, bool death)
{
////////////// before main loop
float startSize, endSize;
float elapsedTime = 0;
if (death)
{
startSize = 1;
endSize = 0;
} else
{
growing = true;
startSize = 0;
endSize = 1;
//Activate collider
treeCollider.enabled = true;
}
////////////// Main loop: Handle growth / shrinking
while (elapsedTime < time)
{
//Transition between start and end size (0 to 1)
float curSize = Mathf.SmoothStep(startSize, endSize, (elapsedTime / time));
//Rotations work different. It's best to define the rate of rotation, the sine function is handy for this (between 0, 1, back to 0)
float curRotationRate = Mathf.Sin(Util.mapVal((elapsedTime / time), 0, 1, 0, Mathf.PI));
//Apply size & rotation
treeTransform.localScale = new Vector3(curSize, curSize, curSize);
treeTransform.RotateAround(treeTransform.position, Vector3.up, curRotationRate * turnDirection);
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
////////////// after main loop
if (death)
{
Destroy(gameObject);
} else
{
//Activate perch targets once growth complete
Util.FindInactiveChild(gameObject, "perchParent").SetActive(true);
growing = false;
if (dead)
{
StartDeath();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment