Effects/ShipRagdoll.cs
/// <summary>
/// Manually driven physics for a ship death ragdoll — flies and spins, then explodes.
/// </summary>
public sealed class ShipRagdoll : Component
{
public Vector3 LinearVelocity { get; set; }
public Vector3 AngularVelocity { get; set; } // degrees/sec
public string ExplosionPrefab { get; set; }
private TimeUntil _explodeTime;
protected override void OnStart()
{
_explodeTime = 0.8f;
}
protected override void OnUpdate()
{
WorldPosition += LinearVelocity * Time.Delta;
LinearVelocity *= MathF.Pow( 0.15f, Time.Delta ); // drag — bleeds off fast
WorldRotation = WorldRotation * Rotation.From( AngularVelocity.EulerAngles * Time.Delta );
if ( _explodeTime )
Explode();
}
private void Explode()
{
var pos = WorldPosition;
if ( !string.IsNullOrEmpty( ExplosionPrefab ) )
{
var prefabFile = ResourceLibrary.Get<PrefabFile>( ExplosionPrefab );
if ( prefabFile != null )
SceneUtility.GetPrefabScene( prefabFile )?.Clone( new CloneConfig
{
Transform = new Transform( pos ),
StartEnabled = true
} );
}
Sound.Play( "explosion", pos );
GameObject.Destroy();
}
}