HazardZone.cs

A game component that acts as a hazard trigger zone. When an object enters the trigger it deals damage to player entities, shakes the camera, detonates explosive barrels, and optionally destroys other physics props that are not tagged as players.

Native Interop
using Sandbox;

public sealed class HazardZone : Component, Component.ITriggerListener
{
	[Property] public float DamagePerTouch { get; set; } = 50f;
	[Property] public bool DestroyPropsOnTouch { get; set; } = true;

	public void OnTriggerEnter( Collider other )
	{
		var rootObj = other.GameObject.Root;

		var health = rootObj.Components.Get<PlayerHealth>();
		if ( health.IsValid() )
		{
			var hitDir = (rootObj.WorldPosition - WorldPosition).Normal;
			health.TakeDamage( DamagePerTouch, hitDir );
			CameraJuice.Instance?.Shake( 20f );
			return;
		}

		if ( DestroyPropsOnTouch )
		{
			var barrel = rootObj.Components.Get<ExplosiveBarrel>();
			if ( barrel.IsValid() )
			{
				barrel.Explode();
				return;
			}

			var rb = rootObj.Components.Get<Rigidbody>();
			if ( rb.IsValid() && !rootObj.Tags.Has( "player" ) )
			{
				rootObj.Destroy();
			}
		}
	}
}