RockDebris component and static Spawn factory for tumbling physics rubble. Spawn builds a GameObject with a model, sphere collider, rigidbody with velocity and random spin, falling back to a dev sphere when the given model is missing. The component plays a few impact sounds scaled by speed and world scale on collisions, then destroys itself after Lifetime.
using System;
/// <summary>
/// A loose chunk of rock: tumbles under physics, knocks when it hits things, and clears itself
/// away after a while. Made with <see cref="Spawn"/> by the breach blast and the orbit-capture jolt.
/// </summary>
[Title( "Rock Debris" ), Category( "Story" ), Icon( "landslide" )]
public sealed class RockDebris : Component, Component.ICollisionListener
{
[Property] public string ImpactSound { get; set; } = "sounds/opening/latch_thud.sound";
/// <summary>Seconds before the chunk removes itself.</summary>
[Property] public float Lifetime { get; set; } = 90f;
Rigidbody _body;
TimeSince _sinceSpawn;
TimeSince _sinceSound = 10f;
int _soundsLeft = 5;
protected override void OnStart()
{
_body = Components.Get<Rigidbody>();
_sinceSpawn = 0;
}
protected override void OnUpdate()
{
if ( _sinceSpawn > Lifetime ) GameObject.Destroy();
}
public void OnCollisionStart( Collision collision )
{
// A few knocks as it bounces, quieter each time, then silence once it has settled.
if ( _soundsLeft <= 0 || _sinceSound < 0.12f || string.IsNullOrEmpty( ImpactSound ) ) return;
float speed = _body.IsValid() ? _body.Velocity.Length : 0f;
if ( speed < 60f ) return;
_soundsLeft--;
_sinceSound = 0;
var sound = StorySound.Play( ImpactSound, WorldPosition );
if ( sound is null ) return;
sound.Volume = Math.Clamp( speed / 500f, 0.15f, 1.2f );
sound.Pitch = Math.Clamp( 1.5f - WorldScale.x * 0.15f, 0.6f, 1.4f ); // big chunks sound lower
}
public void OnCollisionUpdate( Collision collision ) { }
public void OnCollisionStop( CollisionStop collision ) { }
/// <summary>Creates one chunk of rock in the scene, already moving.</summary>
public static GameObject Spawn( Vector3 position, Vector3 velocity, float scale, string modelPath, string materialPath )
{
// The rock prop is not one of this project's own files: if it cannot be found on this
// machine, the breach still throws rubble, as plain rounded lumps.
var model = Model.Load( modelPath );
bool lump = model is null || model.IsError;
if ( lump )
{
model = Model.Load( "models/dev/sphere.vmdl" );
if ( model is null || model.IsError ) return null;
scale *= 0.12f; // the stand-in sphere is 64 across; the rock prop is about 8
}
var go = new GameObject( true, "Rock Debris" );
go.Flags |= GameObjectFlags.NotSaved;
go.Tags.Add( "debris" ); // the lizard doesn't try to cling to these
go.WorldPosition = position;
go.WorldRotation = Rotation.Random;
go.WorldScale = scale;
var renderer = go.Components.Create<ModelRenderer>();
renderer.Model = model;
var material = string.IsNullOrEmpty( materialPath ) ? null : Material.Load( materialPath );
if ( material is not null ) renderer.MaterialOverride = material;
// A simple round collider: the prop's own shape doesn't matter for a tumbling chunk.
go.Components.Create<SphereCollider>().Radius = lump ? 32f : 4f;
var body = go.Components.Create<Rigidbody>();
body.Velocity = velocity;
body.AngularVelocity = Vector3.Random * 7f;
go.Components.Create<RockDebris>();
return go;
}
}