public void ShowNavMeshPoints()
    {
        DateTime start = DateTime.Now;

        //NavMeshHit hitInfo;

        //foreach (GameObject point in navPoints)
        //{
        //    GameObject.DestroyImmediate(point);
        //}

        //if (NavMesh.FindClosestEdge(targetPos, out hitInfo, navMask))
        //{
        //    GameObject o = NavMeshHelper.CreatePoint(hitInfo.position, "Edge", 0.5f, Color.green);
        //    navPoints.Add(o);
        //}
        //else
        //{
        //    //Debug.LogError("FindClosestEdge No");
        //}

        //if (NavMesh.SamplePosition(targetPos, out hitInfo, sampleDistance, navMask))
        //{
        //    GameObject o = NavMeshHelper.CreatePoint(hitInfo.position, "Sample", 0.5f, Color.blue);
        //    navPoints.Add(o);
        //}
        //else
        //{
        //    //Debug.LogError("SamplePosition No");
        //}

        //if (NavMesh.Raycast(targetPos, agent.transform.position, out hitInfo, -1))
        //{
        //    GameObject o = NavMeshHelper.CreatePoint(hitInfo.position, "Raycast", 0.5f, Color.red);
        //    navPoints.Add(o);
        //}
        //else
        //{
        //    //Debug.LogError("No Raycast!!!!");
        //}


        NavMeshHelper.ScalePower = PointScale;

        //var pr = NavMeshHelper.GetClosetPoint(targetPos);
        ////navPoints.Add(NavMeshHelper.CreatePoint(pr, "ClosedPoint", 1f, Color.cyan));

        //TimeSpan time = DateTime.Now - start;
        //Debug.Log("ShowNavMeshPoints:" + time.TotalMilliseconds + "ms");

        NavMeshHelper.GetClosetPointAsync(targetPos, this.name, null, (pr, p) =>
        {
            TimeSpan time = DateTime.Now - start;
            Debug.Log("ShowNavMeshPoints:" + time.TotalMilliseconds + "ms");
        });
    }
Beispiel #2
0
 public static void GetClosetPoint(Vector3 target, NavMeshAgent agent, Action <Vector3> callback)
 {
     NavMeshHelper.GetClosetPointAsync(target, "NPCTest", agent, (p, o) =>
     {
         if (callback != null)
         {
             callback(p);
         }
     });
 }
Beispiel #3
0
    protected void SetSpeedByDistance()
    {
        if (targetPos == Vector3.zero)
        {
            return;                           //总不会那么巧,目标位置就是原点吧。
        }
        //bool r = agent.SetDestination(followTarget.position);
        if (!isPosInfoSet)
        {
            return;
        }
        distance = Vector3.Distance(targetPos, transform.position);
        //if(printDistance)
        //    Debug.Log("distance:" + distance);
        //Log.Info("NavAgentControllerBase.SetSpeedByDistance",string.Format("{0}->{1},distance:{2}", transform.position,targetPos,distance));
        SetSpeed(distance);

        if (enableJump)
        {
            if (distance > MaxDistance || isOnEnableWrap)//距离太远了,直接飞过去
            {
                //if (useWrap)
                //{
                //Vector3 destination = GetDestination(targetPos);
                ////Vector3 destination = NavMeshHelper.GetClosetPointEx(targetPos, agent);
                //if (destination == Vector3.zero)
                //{

                //}
                //else
                //{
                //    var r=agent.Warp(destination);//要用这个
                //    Log.Info("NavAgentControllerBase.SetSpeedByDistance", string.Format("Jump(Warp) distance:{0}|name:{1}|destination:{2}|wrap:{3}|isOnEnableWrap:{4}",
                //        distance , this.name , destination , r, isOnEnableWrap));
                //}

                NavMeshHelper.GetClosetPointAsync(targetPos, this.name, agent, (destination, o) =>
                {
                    var r = agent.Warp(destination);//要用这个
                    Log.Info("NavAgentControllerBase.SetSpeedByDistance", string.Format("Jump(Warp) distance:{0}|name:{1}|destination:{2}|wrap:{3}|isOnEnableWrap:{4}|MaxDistance:{5}",
                                                                                        distance, this.name, destination, r, isOnEnableWrap, MaxDistance));
                });

                isOnEnableWrap = false;
            }
        }
    }
Beispiel #4
0
    public void SetNavAgent(LocationObject o, Action callback)
    {
        Log.Info("PathFindingManager.SetNavAgent", "o:" + o.transform.name);
        if (SystemSettingHelper.locationSetting.EnableNavMesh == false)
        {
            return;                                                            //不启用NavMesh
        }
        if (o == null)
        {
            return;
        }
        if (useNavAgent)
        {
            var agent = o.gameObject.AddComponent <NavAgentController>();//往原来物体上加NavAgentController来控制物体移动
            o.navAgent = agent;
        }
        else if (useFollowNavAgent)
        {
            //if (FollowAgent)
            {
                if (o.navAgentFollow == null)
                {
                    Log.Debug("PathFindingManager.SetNavAgent", string.Format("{0},{1},{2}", o.name, o.transform.position, o.gameObject.activeInHierarchy));

                    //var pos = o.transform.position;
                    //InstantiateNavAgent(o, pos);

                    NavMeshHelper.GetClosetPointAsync(o.transform.position, o.name, null, (pos, obj) =>
                    {
                        InstantiateNavAgent(o, pos);
                        if (callback != null)
                        {
                            callback();
                        }
                    });
                }
            }
        }
        else
        {
            //不使用NavMesh
        }
    }
Beispiel #5
0
    protected void SetDestination(Vector3 pos, Action <bool> callback)
    {
        Log.Info("NavAgentControllerBase.SetDestination", string.Format("{0}->{1},obj:{2}", transform.position, pos, this));
        if (lastPos == pos)//坐标不变,不用计算处理。
        {
            if (callback != null)
            {
                callback(false);
            }
            return;
        }
        //if (lastPos == Vector3.zero)//说明是第一次,直接跳过去吧
        //{
        //    lastPos = pos;
        //    Vector3 posNew=NavMeshHelper.GetClosetPointEx(pos, agent);
        //    if (agent != null)
        //    {
        //        if (agent.gameObject.activeInHierarchy)
        //        {
        //
        //        }
        //        else
        //        {
        //            transform.position = posNew;
        //        }
        //    }
        //    else
        //    {
        //        transform.position = posNew;
        //    }
        //            if (callback != null)
        //    {
        //        callback(true);
        //    }
        //    return;
        //}
        lastPos = pos;

        //Log.Info("NavAgentControllerBase.SetDestination", string.Format("pos:{0},obj:{1}",pos,this));
        if (IsBusyUpdatePosition)
        {
            //Log.Info("NavAgentControllerBase.UpdatePosition", "IsBusyUpdatePosition");
            if (callback != null)
            {
                callback(false);
            }
            return;
        }
        DateTime start = DateTime.Now;

        IsBusyUpdatePosition = true;
        NavMeshHelper.GetClosetPointAsync(pos, this.name, agent, (destination, p) => //多线程异步方式计算,避免影响性能
        {
            try
            {
                //Log.Info("NavAgentControllerBase.SetDestination", string.Format("{0}=>{1},{2}", pos,destination, this));
                if (SystemSettingHelper.IsDebug())//调试模式下,查看接下来的移动方向
                {
                    if (ArcManager.Instance != null)
                    {
                        if (p != null)
                        {
                            ArcManager.Instance.SetLine(gameObject.transform, p.transform);
                        }
                    }
                }

                if (agent != null)
                {
                    if (agent.gameObject.activeInHierarchy)
                    {
                        if (EnableUpdate == false)//代表第一次
                        {
                            Log.Info("NavAgentControllerBase.SetDestination", string.Format("First Wrap!! pos:{0},obj:{1}", pos, this));
                            agent.Warp(destination);
                        }
                        else
                        {
                            bool r = agent.SetDestination(destination); //Agent被关闭或者被销毁,调用这个方法会报错
                            if (r == false)                             //人物物体不在NavMesh上,立刻跳到目标位置
                            {
                                Log.Info("NavAgentControllerBase.SetDestination", string.Format("Wrap pos:{0},obj:{1}", pos, this));
                                //this.transform.position = destination;
                                agent.Warp(destination);//要用这个,用上面那句话,"不在NavMesh上"的问题会一直出现,要用Warp才会重新计算
                            }
                            else
                            {
                                HisPosInfo hisPosInfo = posInfo as HisPosInfo; //假如是历史数据
                                if (hisPosInfo != null)
                                {
                                    var current = hisPosInfo.CurrentPosInfo;
                                    var next    = current.Next;

                                    if (next != null)
                                    {
                                        TimeSpan t = next.Time - current.Time; //下一个点的时间
                                                                               //float distance = Vector3.Distance(next.Vec, current.Vec); //下一个点的距离

                                        //Log.Info("NavAgentControllerBase.UpdatePosition", string.Format("{0}=>{1},time:{2},distance:{3}", current.Vec, next.Vec, time, distance));
                                        this.timespan = t.TotalSeconds;//时间越长,走的越慢
                                    }
                                }
                            }
                        }

                        EnableUpdate = true;
                    }
                    else
                    {
                        Log.Error("NavAgentControllerBase.SetDestination", "agent.gameObject.activeInHierarchy==false:" + this);
                    }
                }
                else
                {
                    Log.Error("NavAgentControllerBase.SetDestination", "agent==null:" + this);
                }
            }
            catch (Exception ex)
            {
                Log.Error("NavAgentControllerBase.SetDestination", "Exception:" + ex);
            }
            finally
            {
                IsBusyUpdatePosition = false;
                TimeSpan time        = DateTime.Now - start;

                if (callback != null)
                {
                    callback(false);
                }
            }



            //Log.Info("NavAgentControllerBase.UpdatePosition", NavMeshHelper.Log);
            //Log.Info("NavAgentControllerBase.SetDestination", "UpdatePosition End time:" +time.TotalMilliseconds+"ms");
        });

        //IsBusyUpdatePosition = true;
        //var destination = NavMeshHelper.GetClosetPoint(pos); //  => //多线程异步方式计算,避免影响性能
        //if (agent != null && agent.gameObject.activeInHierarchy)
        //{
        //    bool r = agent.SetDestination(destination); //Agent被关闭或者被销毁,调用这个方法会报错
        //    Log.Info("NavAgentControllerBase.UpdatePosition", "r:" + r);
        //    if (r == false) //人物物体不在NavMesh上,立刻跳到目标位置
        //    {
        //        //this.transform.position = destination;
        //        agent.Warp(destination);//要用这个,用上面那句话,"不在NavMesh上"的问题会一直出现,要用Warp才会重新计算
        //    }
        //}

        //IsBusyUpdatePosition = false;
        //TimeSpan time = DateTime.Now - start;
        ////Log.Info("NavAgentControllerBase.UpdatePosition", NavMeshHelper.Log);
        //Log.Info("NavAgentControllerBase.UpdatePosition", "UpdatePosition End time:" + time.TotalMilliseconds + "ms");

        //return true;
    }
 public void GetClosetPoint()
 {
     NavMeshHelper.GetClosetPointAsync(this.transform.position, "Test", agent, null);
 }