コード例 #1
0
    // Start is called before the first frame update
    void Start()
    {
        navMeshAgent = GetComponent <NavMeshAgent>();

        if (navMeshAgent == null)
        {
            Debug.Log("navMeshAgent has not been assigned object");
        }
        else
        {
            if (currentNode == null)
            {
                GameObject[] allNodes = GameObject.FindGameObjectsWithTag("Node");

                if (allNodes.Length > 0)
                {
                    while (currentNode == null)
                    {
                        int           random    = Random.Range(0, allNodes.Length);
                        ProximityNode startNode = allNodes[random].GetComponent <ProximityNode>();

                        if (startNode != null)
                        {
                            currentNode = startNode;
                        }
                    }
                }
            }
        }

        setDestination();
    }
コード例 #2
0
    private void setDestination()
    {
        if (nodesVisited > 0)
        {
            ProximityNode nextNode = currentNode.calculateNextNode(previousNode);
            previousNode = currentNode;
            currentNode  = nextNode;
        }

        Vector3 targetDestination = currentNode.transform.position;

        navMeshAgent.SetDestination(targetDestination);
        onRoute = true;
    }
コード例 #3
0
    private void Start()
    {
        //SphereOverlap later
        GameObject[] allNodes = GameObject.FindGameObjectsWithTag("Node");

        foreach (var n in allNodes)
        {
            ProximityNode nextNode = n.GetComponent <ProximityNode>();

            if (nextNode != null)
            {
                if (Vector3.Distance(this.transform.position, nextNode.transform.position) <= proximityRadius && nextNode != this)
                {
                    nodes.Add(nextNode);
                }
            }
        }
    }
コード例 #4
0
//    private readonly Vector3 nodeWireframeSize = new Vector3(2f, 2f, 2f);
//    void OnDrawGizmos()
//    {
//        Vector3 pos = transform.position;
//
//        Gizmos.color = Color.cyan;
//        Gizmos.DrawWireSphere(pos,  2f);
//
//        Gizmos.color = Color.green;
//        Gizmos.DrawWireSphere(pos,  proximityRadius);
//    }

    public ProximityNode calculateNextNode(ProximityNode previousNode)
    {
        if (nodes.Count == 0)
        {
            Debug.Log("No nodes within proximity radius");
            return(null);
        }
        else if (nodes.Count == 1 && nodes.Contains(previousNode))
        {
            return(previousNode);
        }
        else
        {
            ProximityNode nextNode;
            do
            {
                int n = UnityEngine.Random.Range(0, nodes.Count);
                nextNode = nodes[n];
            } while (nextNode == previousNode);

            return(nextNode);
        }
    }