当我们构建的模型是单面模型时,就会出现射线检测不到MeshCollider的问题,对于渲染,我们可以Cull Off来实现双面渲染,而在射线检测时,Unity提供了一个API来控制是否检测背面:Physics.queriesHitBackfaces
案例代码:
csharp
public bool Raycast(out RaycastHit hitOnGround, out RaycastHit hitOnStructure)
{
Physics.queriesHitBackfaces = true;
Ray ray = m_Camera.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitOnGround, m_Camera.farClipPlane * 2, m_GroundLayer);
bool onStructure = Physics.Raycast(ray, out hitOnStructure, m_Camera.farClipPlane * 2, m_StructureLayer);
Physics.queriesHitBackfaces = false;
return onStructure;
}