Ejemplo n.º 1
0
 public override void Start()
 {
     base.Start();
     PathFindingGrid.UpdateNodes();
     startNode = PathFindingGrid.WorldPosToNode(transform.position);
     endNode   = PathFindingGrid.WorldPosToNode(goal.transform.position);
     m_Path    = PathFinder.GeneratePath(startNode, endNode, this);
     // if (!m_Path.IsEmpty)
     // {
     //     m_Path.Visualize(Mathf.Infinity, dropHeight * cellSize);
     // }
     // else
     // {
     //     print("empty");
     // }
 }
Ejemplo n.º 2
0
        public override void Update()
        {
            PathFindingGrid.UpdateNodes();


            startNode = PathFindingGrid.WorldPosToNode(transform.position);
            endNode   = PathFindingGrid.WorldPosToNode(goal.transform.position);


            if (m_BoxCollisionController2D.Collisions.Below)
            {
                m_Path = PathFinder.GeneratePath(startNode, endNode, this);
            }

            if (!m_Path.IsEmpty)
            {
                m_Path.Visualize(Time.deltaTime, dropHeight * cellSize);
            }
            else
            {
                print("empty");
            }
            base.Update();


            if (m_Path.PathNodes.Count <= 1)
            {
                return;
            }
            if (!m_BoxCollisionController2D.Collisions.Below && velocity.y != 0)
            {
                return;
            }
            if (Mathf.Abs(m_Path.GetNextNode().col - startNode.col) > 1)
            {
                velocity = Math.Abs(transform.position.x - startNode.CenterWorldPos.x) < 0.1
                    ? jumpTo(m_Path.GetNextNode().CenterWorldPos)
                    : new Vector2((startNode.CenterWorldPos.x - transform.position.x > 0 ? 1 : -1) * speed, velocity.y);
            }
            else
            {
                velocity = new Vector2((m_Path.GetNextNode().col - startNode.col) * speed, velocity.y);
            }
        }