[コード スニペット] Unity ナビゲーション グリッド プロキシ、プロキシがマウス クリック位置に移動することを実現します

Unity ナビゲーション グリッド エージェント、エージェントがマウスのクリック位置に移動することを実現

    /// <summary>
    /// 人物的导航网格代理
    /// </summary>
    public NavMeshAgent role_NavMeshAgent;
     
    private void Start()
    {
        role_NavMeshAgent.stoppingDistance = 0.5f;//距离0.5米的时候停止移动

        var mainCamera = Camera.main;
        if (mainCamera == null)
        {
            Debug.Log("主摄像机为空");
        }
    }

    private void Update()
    {
        RaycastHit hitInfo;

        if (Input.GetMouseButtonDown(0))
        {
            //从主摄像机发射一条射线至鼠标点击的位置。
            Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);

            //检查射线是否碰撞到物体
            if (Physics.Raycast(ray1, out hitInfo, 100000))
            {
                 if (Vector3.Distance(role_NavMeshAgent.gameObject.transform.position, hitInfo.point) < 5.0f)//两个点的距离太远
                {
                    role_NavMeshAgent.speed = 3.5f;

                    Vector3 targetpos = hitInfo.point;
                    role_NavMeshAgent.SetDestination(targetpos);
                    Debug.Log("正

 - List item

在前往目标地点");
                }
                else
                {
                    Debug.Log("距离太远了");

                }

            }

        }


        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("停止导航");
            role_NavMeshAgent.speed = 0.0f;//速度设置为0
            role_NavMeshAgent.ResetPath();//清除导航路径的计算
        }


    }



おすすめ

転載: blog.csdn.net/GoodCooking/article/details/132857895
おすすめ