예제 #1
0
 /// <summary>
 /// 调整坐标位置,如果坐标的y在地面以下的话,就调整
 /// </summary>
 protected void AdjustPosition()
 {
     if (GameWorld.isLoadingScene)
     {
         return;
     }
     if (transform.position.y < -100 && transform.position.y > -9000)
     {
         Vector3 temp;
         bool    hasHit = UnityTools.GetPointInTerrain(transform.position.x, transform.position.z, out temp);
         if (!hasHit)
         {
         }
         else
         {
             transform.position = temp;
         }
     }
 }
    public NavMeshPath GetPathByTarget(Vector3 target)
    {
        if (m_navGo == null)
        {
            m_navGo = new GameObject();
        }
        NavMeshPath path = new NavMeshPath();

        target = GetPointCloseToTheMesh(target);
        if (!ResetPositionToCLoseToNavMesh())
        {
            Debug.LogWarning("不能发现Navmesh");
            return(path);
        }
        try
        {
            NavMesh.CalculatePath(m_navGo.transform.position, target, -1, path);
            if (path.corners.Length <= 0)
            {
                m_navAgent.enabled = true;

                m_navAgent.SetDestination(target);
                path = m_navAgent.path;
                if (path.corners.Length <= 0)
                {
                    Debug.LogError("Path's corner Count <= 0");
                }
                m_navAgent.Stop();
            }
        }
        catch
        {
        }
        for (int i = 0; i < path.corners.Length; i++)
        {
            UnityTools.GetPointInTerrain(path.corners[i].x, path.corners[i].z, out path.corners[i]);
        }

        return(path);
    }
예제 #3
0
        public virtual void UpdatePosition()
        {
            Vector3 point = Vector3.zero;

            if (Transform)
            {
                if (UnityTools.GetPointInTerrain(Position.x, Position.z, out point))
                {
                    Transform.position = new Vector3(point.x, point.y + 0.3f, point.z);
                    if (Rotation != Vector3.zero)
                    {
                        Transform.eulerAngles = new Vector3(0, Rotation.y, 0);
                    }
                }
            }
            else
            {
                var myself = this as EntityMyself;
                if (myself != null)
                {
                    DataSceneConfig config = GameData <DataSceneConfig> .dataMap[myself.SceneId];
                    Vector3         bornPoint;
                    if (UnityTools.GetPointInTerrain(config.EnterX, config.EnterZ, out bornPoint))
                    {
                        Transform.position = new Vector3(bornPoint.x, bornPoint.y + 0.5f, bornPoint.z);
                    }
                    else
                    {
                        Transform.position = bornPoint;
                    }
                }
                else
                {
                    Transform.position = new Vector3(point.x, point.y + 1f, point.z);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 移动到指定目的
        /// </summary>
        /// <param name="target"></param>
        /// <param name="needToAdjustPosY"></param>
        public override void MoveTo(Vector3 target, bool needToAdjustPosY = true)
        {
            if (path != null && m_cornersIdx < path.corners.Length)
            {
                if (!canMove)
                {
                    return;
                }
            }
            //如果已经到达目的地,并且不需要调整位置
            if (m_isMovingOn && !needToAdjustPosY)
            {
                return;
            }
            else
            {
                //停止上个还没有寻完的路
                StopNav();
            }
            if (needToAdjustPosY)
            {
                bool hasHit = UnityTools.GetPointInTerrain(target.x, target.z, out target);
                if (hasHit == false)
                {
                    return;
                }
            }
            if (!isMovingToTarget)
            {
                path           = m_navmeshHelper.GetPathByTarget(target);
                targetToMoveTo = target;
                m_cornersIdx   = 1;
            }
            if (path.corners.Length < 2)
            {
                StopNav();
                GameWorld.thePlayer.Idle();
                return;
            }
            isMovingToTarget = true;
            moveDirection    = (path.corners[m_cornersIdx] - transform.position).normalized;
            transform.LookAt(new Vector3(path.corners[m_cornersIdx].x, transform.position.y, path.corners[m_cornersIdx].z));
            float dis  = Vector3.Distance(transform.position, path.corners[m_cornersIdx]);
            float step = 8 * Time.deltaTime;

            if (step + 0.1f > dis && m_cornersIdx < path.corners.Length - 1)
            {
                collisionFlags = m_characterController.Move(transform.forward * dis);
                m_cornersIdx++;
                transform.LookAt(new Vector3(path.corners[m_cornersIdx].x, transform.position.y, path.corners[m_cornersIdx].z));
            }
            else if (m_cornersIdx == path.corners.Length - 1 && step + 0.1f + m_stopDistance > dis)
            {
                float tempDis;
                tempDis        = dis - m_stopDistance;
                tempDis        = tempDis > 0 ? tempDis : 0;
                collisionFlags = m_characterController.Move((path.corners[m_cornersIdx] - transform.position).normalized * tempDis);
                StopNav();
                tempDis = Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(targetToMoveTo.x, targetToMoveTo.z));
                if (tempDis > 0.2 + m_stopDistance)
                {
                    GameWorld.thePlayer.Idle();
                    //EventDispatcher.TriggerEvent(ON_MOVE_TO_FALSE, transform.gameObject, targetToMoveTo, tempDis);
                }
                else
                {
                    GameWorld.thePlayer.Idle();
                    //EventDispatcher.TriggerEvent(ON_MOVE_TO, transform.gameObject, targetToMoveTo);
                }
            }
        }