NavMesh名字、层索引、层值之间的转换

    // Nav层名字-->层的值,1、2、4、8、16
    public static int AgentLayerNameToValue(string name)
    {
        int idx = NavMesh.GetNavMeshLayerFromName(name);
        return 0x1 << idx;
    }

    // Nav层名字-->层索引,0、1、2、3、4
    public static int AgentLayerNameToIndex(string name)
    {
        return NavMesh.GetNavMeshLayerFromName(name);
    }

    // 获取角色当前所在的层值,1、2、4、8、16
    public static int GetAgentLayer(NavMeshAgent agent)
    {
        NavMeshHit hit;
        // 不要使用agent.raduis为采样范围,因为当该值为0时,函数将返回0
        bool reach = NavMesh.SamplePosition(agent.transform.position, out hit, 1f, -1);
        return hit.mask;
    }

    public static Vector3 SampleNavMeshPosition(Vector3 logicPosition, out bool reachable)
    {
        NavMeshHit hit;
        reachable = NavMesh.SamplePosition(logicPosition, out hit, 1f, -1);
        return reachable ? hit.position : logicPosition;
    }

    // 开启导航层
    public static void EnableNavMeshLayer(NavMeshAgent agent, string layerName)
    {
        if (agent == null)
            return;

        int layerValue = NavMesh.GetNavMeshLayerFromName(layerName);
        if (layerValue == -1)
            return;

        int mask = agent.walkableMask | 0x1 << layerValue;
        WalkArbiter.SetWalkableMask(agent, mask);
    }

    // 关闭导航层
    public static void DisableNavMeshLayer(NavMeshAgent agent, string layerName)
    {
        if (agent == null)
            return;

        int layerValue = NavMesh.GetNavMeshLayerFromName(layerName);
        if (layerValue == -1)
            return;

        int mask = agent.walkableMask & ~(0x1 << layerValue);

        WalkArbiter.SetWalkableMask(agent, mask);
    }

    // 检查某个层是否为开启
    public static bool IsNavMeshLayerOpen(NavMeshAgent agent, string layerName)
    {
        int layerValue = NavMesh.GetNavMeshLayerFromName(layerName);
        if (layerValue == -1)
            return true;

        int ret = agent.walkableMask & (0x1 << layerValue);
        return ret > 0 ? true : false;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值