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;
}
相关推荐
郝学胜-神的一滴1 小时前
中级OpenGL教程 023:Assimp模型加载全解——从源码到架构的骈文探秘
c++·unity·游戏引擎·cmake·unreal engine·opengl
xcLeigh19 小时前
Unity基础:GameObject与Component——Unity核心架构思想彻底理解
unity·教程·component·gameobject
郝学胜-神的一滴1 天前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl
weixin_424294672 天前
Unity的测试Edit Mode和Play Mode,有什么区别?
unity
Python私教2 天前
我用 AI 做出了第一个 Godot 贪吃蛇
人工智能·游戏引擎·godot
玖玥拾2 天前
Unity 3D 笔记(八)ScrollRect 滚动视图、NavMesh 自动寻路系统
笔记·3d·unity
淡海水3 天前
06-04-YooAsset源码-Unity加密解密服务
前端·unity·性能优化·c#·游戏引擎·yooasset
cd_949217213 天前
Unity游戏角色资产怎么快速制作?用V2Fun跑通生成、绑定和导入测试
游戏·unity·游戏引擎