UnitShieldVfx.cs

A component that visualizes a unit shield using a ParticleEffect. It sets the particle scale based on the unit radius, randomizes a wobble offset and speed, and updates particle alpha and local scale over time to animate spawn and wobble.

Native Interop
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 );
	}
}