// trail
trailRenderer = GameObject.AddComponent<TrailRenderer>();
// Configure the TrailRenderer's properties
trailRenderer.time = 20.0f; // Duration of the trail
trailRenderer.startWidth = 0.5f; // Width of the trail at the start
trailRenderer.endWidth = 0.0f; // Width of the trail at the end
// a material to the trail
trailRenderer.material = new Material(Shader.Find("Sprites/Default"));
// Set the trail color over time
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.red, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) }
);
trailRenderer.colorGradient = gradient;