unity
runtime_error
ai_generated
true
InvalidOperationException: 如果未将碰撞器设置为 QueryTriggerInteraction.Ignore,Physics.Raycast 查询可能会多次击中同一碰撞器
InvalidOperationException: Physics.Raycast query may hit the same collider multiple times if the collider is not set to QueryTriggerInteraction.Ignore
ID: unity/physics-query-hits-self
91%修复率
85%置信度
1证据数
2024-06-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Unity 2022.3 | active | — | — | — |
| Unity 2023.1 | active | — | — | — |
| Unity 2023.2 | active | — | — | — |
| Unity 6000.0 | active | — | — | — |
根因分析
当碰撞器是触发器且 QueryTriggerInteraction.UseGlobal 设置时,Physics.RaycastNonAlloc 调用会返回重复命中,导致数组溢出。
English
A Physics.RaycastNonAlloc call returns duplicate hits when the collider is a trigger and QueryTriggerInteraction.UseGlobal is set, causing array overflow.
官方文档
https://docs.unity3d.com/ScriptReference/Physics.RaycastNonAlloc.html解决方案
-
Set QueryTriggerInteraction.Ignore in the RaycastNonAlloc call to ignore trigger colliders.
-
Use Physics.RaycastAll instead and deduplicate hits by collider instance ID.
无效尝试
常见但无效的做法:
-
90% 失败
The error occurs because the same collider is hit multiple times, not because the buffer is too small.
-
70% 失败
Changing isTrigger alters collision behavior and may not be acceptable for the game design.