示例#1
0
    IEnumerator pathLoop()
    {
        while (true)
        {
            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                PathfindingNode curNode = currentNodeTarget;

                if (currentNodeTarget.NeighborCount() - 1 > 2)
                {
                    yield return(new WaitForSeconds(0.25f));
                }

                currentNodeTarget = currentNodeTarget.ChooseNeighbor(lastTarget);
                lastTarget        = curNode;
                agent.SetDestination(currentNodeTarget.transform.position);
            }
            yield return(null);
        }
    }
    // Behaviors

    // uses the nav mesh to pick nieghboring location nodes and travel between them.
    IEnumerator PatrollingBehavior()
    {
        agent.speed = patrolSpeed;
        // if the path has been reset
        float closestDistance = Mathf.Infinity;

        if (currentNodeTarget == null)
        {
            foreach (PathfindingNode node in pfNodes)
            {
                float distance = Vector3.Distance(transform.position, node.transform.position);
                if (distance < closestDistance)
                {
                    closestDistance   = distance;
                    currentNodeTarget = node;
                }
            }

            agent.SetDestination(currentNodeTarget.transform.position);
        }

        // if we were on the path last time
        if (agent.remainingDistance <= agent.stoppingDistance)
        {
            PathfindingNode curNode = currentNodeTarget;

            if (currentNodeTarget.NeighborCount() - 1 > 2)
            {
                yield return(new WaitForSeconds(0.25f));
            }

            currentNodeTarget = currentNodeTarget.ChooseNeighbor(lastTarget);
            lastTarget        = curNode;
            agent.SetDestination(currentNodeTarget.transform.position);
        }

        yield return(null);
    }