using UnityEngine;

public class SkidFade : MonoBehaviour 
{
    public float Lifetime = 3f;
    public float FadeTime = 2f;

    private SpriteRenderer rend;
    private float spawnTime;
    private float origAlpha;

    void Start()
    {
        rend = GetComponent<SpriteRenderer>();
        spawnTime = Time.time;
        origAlpha = rend.color.a;
    }

    void Update()
    {
        float age = Time.time - spawnTime;

        if (age > Lifetime + FadeTime)
        {
            Destroy(gameObject);
            return;
        }
        else if (age > Lifetime)
        {
            float factor = (age- Lifetime) / FadeTime;
            rend.color = new Color(1, 1, 1, origAlpha*(1 - factor));
        }
    }
}
