Unity射线检测不到MeshCollider的原因

当我们构建的模型是单面模型时,就会出现射线检测不到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;
}
相关推荐
RReality3 小时前
【Unity UGUI】Toggle / ToggleGroup 与 Dropdown
ui·unity·游戏引擎·图形渲染·材质
雪儿waii4 小时前
Unity 中的 InvokeRepeating 详解
unity·游戏引擎
mxwin4 小时前
Unity Shader 程序化生成:Shader 中的数学宇宙
unity·游戏引擎
雪儿waii5 小时前
Unity 中的 Quaternion(四元数)详解
unity·游戏引擎
RReality5 小时前
【Unity UGUI】ScrollRect 与 Scrollbar 深度用法
unity·游戏引擎
人邮异步社区5 小时前
如何自学游戏引擎的开发?
unity·程序员·游戏引擎
郝学胜-神的一滴7 小时前
[简化版 Games 101] 计算机图形学 05:二维变换下
c++·unity·图形渲染·three.js·opengl·unreal
mxwin20 小时前
Unity URP 热更新兼容性:Shader 在 IL2CPP 打包下的注意事项
unity·游戏引擎
mxwin1 天前
Unity shader中TransformWorldToShadowCoord原理解析
unity·游戏引擎·shader
mxwin1 天前
Unity Shader 中 ShadowCaster的作用和疑问
unity·游戏引擎