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;
}
相关推荐
新的瑞拉公主21 小时前
Unity游戏发布微信小游戏:从构建到提审
unity·webgl·微信小游戏·开放数据域
狂人开飞机2 天前
14、游戏手感与视觉打磨
游戏·游戏引擎·godot
狂人开飞机2 天前
15、音频系统
游戏引擎·godot
狂人开飞机2 天前
16、粒子线条与视觉特效
游戏引擎·godot
心前阳光2 天前
Unity发布PC软件图标不能改变
unity·游戏引擎
an86950013 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
xcLeigh3 天前
Unity基础:使用Transform控制物体移动——Translate与position详解
unity·游戏引擎
FairGuard手游加固3 天前
Unity小游戏加密:global-metadata.dat与AssetBundle保护
游戏·unity·游戏引擎
狂人开飞机3 天前
11、UI系统
游戏引擎·godot
真鬼1233 天前
【Unity AI】Untiy链接cursor与MCP
unity·游戏引擎