unity runtime_error ai_generated true

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

Also available as: JSON · Markdown · 中文
91%Fix Rate
85%Confidence
1Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2022.3 active
Unity 2023.1 active
Unity 2023.2 active
Unity 6000.0 active

Root Cause

A Physics.RaycastNonAlloc call returns duplicate hits when the collider is a trigger and QueryTriggerInteraction.UseGlobal is set, causing array overflow.

generic

中文

当碰撞器是触发器且 QueryTriggerInteraction.UseGlobal 设置时,Physics.RaycastNonAlloc 调用会返回重复命中,导致数组溢出。

Official Documentation

https://docs.unity3d.com/ScriptReference/Physics.RaycastNonAlloc.html

Workarounds

  1. 95% success Set QueryTriggerInteraction.Ignore in the RaycastNonAlloc call to ignore trigger colliders.
    Set QueryTriggerInteraction.Ignore in the RaycastNonAlloc call to ignore trigger colliders.
  2. 92% success Use Physics.RaycastAll instead and deduplicate hits by collider instance ID.
    Use Physics.RaycastAll instead and deduplicate hits by collider instance ID.

中文步骤

  1. Set QueryTriggerInteraction.Ignore in the RaycastNonAlloc call to ignore trigger colliders.
  2. Use Physics.RaycastAll instead and deduplicate hits by collider instance ID.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The error occurs because the same collider is hit multiple times, not because the buffer is too small.

  2. 70% fail

    Changing isTrigger alters collision behavior and may not be acceptable for the game design.