其他分享
首页 > 其他分享> > NavMesh-areaMask, cost

NavMesh-areaMask, cost

作者:互联网

# 这2个在Navigation的Areas页签设置

 

# 假设有黑色的cat和白色的dog同时要从原点,走到对面ground2上去,cat只能走bridge,dog可以走bridge和water

# 水流速度很慢(cost设为2)时,cat走bridge(黄色), dog走water(蓝色)

# 水流很急(cost设为3)时, cat和dog都走bridge

# 挂在cat和dog上的测试脚本

using UnityEngine;
using UnityEngine.AI;

public class NavMeshTest : MonoBehaviour
{
    private NavMeshAgent _agent;

    void Awake()
    {
        _agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition); //摄像机方向发射1条射线
            Debug.DrawRay(ray.origin, ray.direction * 20, Color.yellow); //画出这条射线

            const int maxDistance = 50;
            if (Physics.Raycast(ray, out var hit, maxDistance))
            {
                _agent.destination = hit.point;
                Debug.DrawLine(ray.origin, hit.point, Color.red);
            }
        }
    }

}

 

【参考】

Unity NavMesh (导航网格)初探 - 简书 (jianshu.com)

unity自带寻路Navmesh入门教程(三)_阿赵3D的博客-CSDN博客_unity的navmesh

 

标签:bridge,NavMesh,hit,dog,cat,cost,areaMask,ray
来源: https://www.cnblogs.com/sailJs/p/16366796.html