Skip to content

Animation

Nick Stapleton edited this page Jun 10, 2019 · 6 revisions

Lives in Scripts/Terrain/planeCollision.cs

This handles the asynchronous lerping of color. This probably overly complicates the code and is responsible for the shield color flicker bug, but... it sounded like a good idea at the time and it works for the color change on the planeCollision.

Abstract

Code

public class Animation
{
    // Indicates if the animator is currently busy with a thread. Init is false.
    bool busy;

    // Lerps material's color to value without threads.
    // FrameMulti skips frames
    // ie, frameMulti = 4 means color will be set 4 times less than normal lerp. This is to increase performance.
    public void lerpColorWithoutThread(float lerpTime, Material mat, Color targetColor, bool locked = false, float frameMulti = 2f);

    // Runs a function after waitTime.
    // Used for calls backs 
    public IEnumerator WaitForSecondsThenExecute(Action method, float waitTime);

    // Runs a function after waitTime.
    // Used for calls backs 
    // source https://answers.unity.com/questions/516798/executing-an-action-after-a-coroutine-has-finished.html
    public IEnumerator WaitForSecondsThenExecute(Action method, float waitTime);

    // Changing the upper and lower bounds will increase the spread at which this coroutine yields.
    // Ie, makes it look different.
    public IEnumerator lerpColor(float lerpTime, Material mat, Color targetColor, bool locked = false, float frameMulti = 2f, float juiceLowerRange = 1f, float juiceUpperRange = 2f);
}

Usage

From planeCollision.OnTriggerEnter()

void OnTriggerEnter(Collider other)
{
    if (!other.gameObject.ToString().Contains("Projectile"))
    {
        IGeo[] res = other.gameObject.GetComponents<IGeo>();
        if (res.Length > 0)
        {
            IGeo geo = res[0];
            if (geo != null)
            {
                // a primitive synch lock
                if (animLock == false)
                {
                    StartCoroutine(anim.lerpColor(animTime, GetComponent<Renderer>().material, geo.GetColor(), animLock));
                }
            }
        }
    }
}