Ejemplo n.º 1
0
    protected void Move()
    {
        if (path != null && path.points.Count > 0)
        {
            var next = path.points.First();

            if (next != null)
            {
                if (pos2d == next)
                {
                    path.points.Remove(next);

                    if (path.points.Count > 0)
                    {
                        next = path.points.First();
                        if (next != null)
                        {
                            nextPos = next;
                        }
                    }
                }
                else
                {
                    nextPos = next;
                }
            }
        }
        else
        {
            path = null;
        }

        if (nextPos != IPosition.zero &&
            GameManager.instance.SameDestination(this, nextPos))
        {
            nextPos = IPosition.zero;
            path    = null;
        }

        if (nextPos != IPosition.zero &&
            Pathfinding.CanWalkToSquare(this, nextPos) == false)
        {
            nextPos = IPosition.zero;
            path    = null;
        }

        if (nextPos != IPosition.zero)
        {
            double distance = Vector3.Distance(transform.position.To2D(), nextPos.ToVector());

            if (distance > 0.05)
            {
                Vector3 dir = (nextPos.ToVector() - transform.position).normalized;
                dir.y = 0;

                if (distance >= .1)
                {
                    transform.position += dir * speed * Time.deltaTime;

                    anim.SetFloat("Speed", speed);
                }
                else
                {
                    transform.position = new Vector3(0, transform.position.y, 0)
                                         + pos2d.ToVector();
                }

                Quaternion newRotation = Quaternion.LookRotation(dir);
                rigidbody.MoveRotation(newRotation);
            }
            else
            {
                anim.SetFloat("Speed", 0);
            }
        }
    }