Raycast / Hit Cache

GetRaycastHits

public IEnumerable<Vector3> GetRaycastHits() => m_HitCache.ConvertAll(h => h.point);

Purpose: Get all hit points (world space) from the last rebuild. Returns: IEnumerable<Vector3> Notes: Handy for debug rendering or custom analytics.


GetHitTransforms

public List<Transform> GetHitTransforms()
{
    List<Transform> list = new();
    foreach (var h in m_HitCache)
        if (h.transform && !list.Contains(h.transform))
            list.Add(h.transform);
    return list;
}

Purpose: Unique set of Transforms that were hit. Returns: List<Transform> Notes: Which objects were touched by the projection.


GetHitMaterialSet

public HashSet<Material> GetHitMaterialSet()
{
    HashSet<Material> set = new();
    foreach (var h in m_HitCache)
        if (h.collider && h.collider.TryGetComponent(out Renderer r) && r.sharedMaterials != null)
            foreach (var mat in r.sharedMaterials)
                if (mat) set.Add(mat);
    return set;
}

Purpose: Unique set of Materials belonging to hit renderers. Returns: HashSet<Material> Notes: Useful for material-specific logic or reporting.


GetLayerHitStatistics

public Dictionary<int, int> GetLayerHitStatistics()
{
    Dictionary<int, int> dict = new();
    foreach (var h in m_HitCache)
    {
        int layer = h.collider ? h.collider.gameObject.layer : -1;
        dict.TryAdd(layer, 0);
        dict[layer]++;
    }
    return dict;
}

Purpose: Map Layer → hit count. Returns: Dictionary<int,int> Notes: Helps tune masks and performance per-layer.


ClearHitCache

public void ClearHitCache() => m_HitCache.Clear();

Purpose: Clear cached hits (does not rebuild). Returns: void


RegisterOnHitCallback

public void RegisterOnHitCallback(Func<RaycastHit, bool> filter, Action<RaycastHit> callback)
{
    if (filter == null || callback == null) return;
    m_HitFilters.Add(filter);
    m_OnHitCallbacks.Add(hit => { if (filter(hit)) callback(hit); });
}

Purpose: Register a filter + callback to be invoked for each cached hit after a rebuild. Parameters:

  • filter — predicate; only hits returning true trigger the callback.

  • callback — invoked for each passing hit. Returns: void Notes: You can register multiple filtered callbacks; great for analytics and selective processing.


Last updated