I have some code I run when I fell a tree in my game. Basically it picks a random direction and then falls. However atm it linear and doesnt look very realistic. What Id want is for it to start slow, then speed up the more it's fallen.
I've been experimenting with animation curves but none of the material I can find works with my code:
public void FellTree()
{
// pick a random point on the circle to match the up vector
Vector2 pointOnCircle = UnityEngine.Random.insideUnitCircle * transform.localScale.y;
// find the fall point, assuming the pivot of the object is at the bottom
Vector3 fallPoint = transform.position +
pointOnCircle.x * transform.right +
pointOnCircle.y * transform.forward;
// find the target up vector
Vector3 updatedUpVector = Vector3.Normalize(fallPoint - transform.position);
// Start the coroutine to tilt the up vector to the desired target
StartCoroutine(UpdateUpVector(updatedUpVector, 1, 0.001f));
}
IEnumerator UpdateUpVector(Vector3 upVector, float speed, float threshold = 0.001f)
{
// the target vector and up vector would get closer to each other until the threshold is hit
while (Vector3.Distance(upVector, transform.up) > threshold)
{
transform.up = Vector3.Lerp(transform.up, upVector, speed * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
}
Using animationCurve.Evaluate(float t)
in the lerp should be what I need to do, but not working. How can I add an animation curve to my lerp?