UnitShieldVfx.cs

A component that attaches a shield particle effect to a unit. It configures the particle scale from the unit radius, randomizes a wobble offset and speed, and animates the particle alpha and local scale over the first 0.25 seconds after spawn.

using System;
using Sandbox;

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

	private float _timeOffset;
	private TimeSince _timeSinceSpawn;
	private float _wobbleSpeed;

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

		var unit = Transform.GameObject.Parent.GetComponent<Unit>();
		ParticleEffect.Scale = 3f * unit.Radius;

		_timeOffset = Game.Random.Float( 0f, 10f );
		_wobbleSpeed = Game.Random.Float( 4f, 5f );

		_timeSinceSpawn = 0f;
	}

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

		ParticleEffect.Alpha = Utils.Map( _timeSinceSpawn, 0f, 0.25f, 0f, 1f, EasingType.QuadOut );

		var scale = ( 1f + Utils.FastSin( _timeOffset + _timeSinceSpawn * _wobbleSpeed ) * 0.05f ) * Utils.Map( _timeSinceSpawn, 0f, 0.25f, 1.33f, 1f, EasingType.QuadOut );
		LocalScale = new Vector3( scale );
	}
}