Exemplo n.º 1
0
    void OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position, Vector3.one);
        foreach (GameObject neighbor in neighbors)
        {
            Gizmos.DrawLine(transform.position, neighbor.transform.position);
            Gizmos.DrawWireSphere(neighbor.transform.position, 0.25f);
        }

        if (goal)
        {
            Gizmos.color = Color.green;
            GameObject         current = gameObject;
            Stack <GameObject> path    = DijkstraAlgorythm.Dijkstra(GameObject.FindGameObjectsWithTag("Node"), gameObject, goal);

            Debug.Log("Got here:");
            Debug.Log(path);
            foreach (GameObject obj in path)
            {
                Debug.Log("Got here also!");
                Gizmos.DrawWireSphere(obj.transform.position, 1.0f);
                Gizmos.DrawLine(current.transform.position, obj.transform.position);
                current = obj;
            }
        }
    }
Exemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");

        //If the dude can be "seen" (Via raycast) then chase
        RaycastHit hit;


        Physics.SphereCast(transform.position, sphereCastRadius, player.transform.position - transform.position, out hit);

        if (hit.collider.tag == "Player")
        {
            Vector3 playerPosition  = player.transform.position;
            Vector3 playerDirection = playerPosition - transform.position;
            playerDirection.y = 0.0f;
            Vector3 normalizedPlayerDirection = playerDirection.normalized;
            transform.position += transform.forward * speed * Time.deltaTime;
            transform.rotation  = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(normalizedPlayerDirection), turnSpeed * Time.deltaTime);
            gameObject.GetComponent <CurrentNode>().currentNode = null;
        }
        else
        {
            //Traverse to next node in path to player node
            Stack <GameObject> path = DijkstraAlgorythm.Dijkstra(
                GameObject.FindGameObjectsWithTag("Node"),
                gameObject.GetComponent <CurrentNode>().currentNode,
                player.GetComponent <CurrentNode>().currentNode);
            if (path != null)
            {
                goal = path.Pop();
                Vector3 goalPosition  = goal.transform.position;
                Vector3 goalDirection = goalPosition - transform.position;
                goalDirection.y = 0.0f;
                Vector3 normalizedGoalDirection = goalDirection.normalized;
                transform.position += transform.forward * speed * Time.deltaTime;
                transform.rotation  = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(normalizedGoalDirection), turnSpeed * Time.deltaTime);
            }
        }
    }