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;
}
相关推荐
我的offer在哪里5 小时前
示例 Unity 项目结构(Playable Game Template)
unity·游戏引擎
淡海水8 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
在路上看风景8 小时前
4.6 显存和缓存
unity
Zik----11 小时前
简单的Unity漫游场景搭建
unity·游戏引擎
在路上看风景21 小时前
4.5 顶点和片元
unity
在路上看风景1 天前
31. Unity 异步加载的底层细节
unity
天人合一peng1 天前
Unity中做表头时像work中整个调整宽窄
unity
小李也疯狂2 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的2 天前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y2 天前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表