# InvalidOperationException: 如果碰撞器未设置为，Physics.Raycast 查询可能会多次命中同一碰撞器

- **ID:** `unity/raycast-multiple-collider`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

当碰撞器有多个接触点或未配置 'QueryTriggerInteraction.Ignore' 或正确的层过滤时，RaycastNonAlloc 查询会为单个碰撞器返回重复命中。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2022.3 | active | — | — |
| Unity 2023.1 | active | — | — |
| Unity 2023.2 | active | — | — |

## 解决方案

1. ```
   Use 'HashSet<Collider>' to deduplicate hits after calling RaycastNonAlloc. Example: HashSet<Collider> hitColliders = new HashSet<Collider>(); foreach (var hit in results) hitColliders.Add(hit.collider);
   ```
2. ```
   Set 'QueryTriggerInteraction.Ignore' in the raycast call and use layer masks to exclude duplicate-prone colliders. Example: Physics.RaycastNonAlloc(ray, results, maxDistance, layerMask, QueryTriggerInteraction.Ignore);
   ```

## 无效尝试

- **** — Triggers can still cause duplicate entries in non-alloc queries; the issue is about query filtering, not trigger state. (60% 失败率)
- **** — RaycastAll also returns duplicates; the problem is generic to all raycast methods when colliders have multiple shapes. (80% 失败率)
- **** — Contact offset affects collision detection resolution, not raycast query deduplication. (70% 失败率)
