UnitShockVfx.cs

A component that manages a ParticleEffect's lifetime and fades its alpha over time. It initializes Lifetime to 1 second and on each update sets ParticleEffect.Alpha using a mapped easing curve based on TimeSince spawn.

Native Interop
using System;
using Sandbox;

public class UnitShockVfx : Component
{
	[Property] public ParticleEffect ParticleEffect { get; set; }

	private TimeSince _timeSinceSpawn;
	public float Lifetime { get; set; }

	protected override void OnStart()
	{
		base.OnStart();

		_timeSinceSpawn = 0f;
		Lifetime = 1f;
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		ParticleEffect.Alpha = Utils.Map( _timeSinceSpawn, 0f, Lifetime, 1f, 0f, EasingType.ExpoOut );
	}
}