Skip to content

Triggering via Code ​

This page introduces three practical implementation patterns for calling the OnTrigger method from a script (C#) to execute destruction at any desired timing or condition.

Using BaseDestruction

In the following sample codes, the base class BaseDestruction is used to reference the component instead of a specific class like InstantDestructionSimple. This ensures excellent extensibility, allowing your calling scripts to continue working without modification even if you change the type of the destruction component in the future.

1. Simple Destruction ​

This is the most basic pattern. It triggers destruction on a target object that has been referenced in advance via the Inspector.

csharp
using UnityEngine;
using InstantDestruction;

public class SimpleDestroyer : MonoBehaviour
{
    [SerializeField] private BaseDestruction targetDestruction;

    public void ExecuteDestruction()
    {
        if (targetDestruction != null)
        {
            // Trigger destruction from the center of the object
            targetDestruction.OnTrigger();
        }
    }
}

2. Area Explosion ​

This pattern assumes an explosive device like a bomb. It uses Physics.OverlapSphere to detect destructible objects within a certain radius from specific coordinates and destroys them all at once. By passing the explosion's center coordinates as an argument, fragments will scatter originating from that point.

csharp
using UnityEngine;
using InstantDestruction;

public class BombExplosion : MonoBehaviour
{
    [SerializeField] private Transform targetTransform;
    [SerializeField] private float explosionRadius = 5f;

    public void Explode()
    {
        Vector3 explosionCenter = targetTransform.position;

        // Find all colliders within the explosion radius
        Collider[] hitColliders = Physics.OverlapSphere(explosionCenter, explosionRadius);

        foreach (Collider hitCollider in hitColliders)
        {
            // Check if the object has the BaseDestruction component
            if (hitCollider.TryGetComponent<BaseDestruction>(out var destruction))
            {
                // Trigger destruction with the explosion center as the origin
                destruction.OnTrigger(explosionCenter);
            }
        }
    }
}

3. Raycast Shooting ​

This pattern assumes firing a weapon like a gun. By using a raycast (Physics.Raycast) to get the impact point and passing those coordinates to OnTrigger, you can accurately blow fragments away from the exact spot the bullet hit.

csharp
using UnityEngine;
using InstantDestruction;

public class RaycastShooter : MonoBehaviour
{
    [SerializeField] private Transform gunMuzzle;
    [SerializeField] private float shootRange = 100f;

    public void Shoot()
    {
        // Shoot a ray from the muzzle forward
        if (Physics.Raycast(gunMuzzle.position, gunMuzzle.forward, out RaycastHit hit, shootRange))
        {
            // Check if the hit object has the BaseDestruction component
            if (hit.collider.TryGetComponent<BaseDestruction>(out var destruction))
            {
                // Trigger destruction using the exact hit point as the explosion center
                destruction.OnTrigger(hit.point);
            }
        }
    }
}