Example #1
0
    // Update is called once per frame
    void Update()
    {
        // check the time in the timer, then attack the player
        if (timeBetweenAttacks <= 0 && player != null)
        {
            AttackPlayer();
            timeBetweenAttacks = startTimeBetweenAttacks;
        }
        else
        {
            timeBetweenAttacks -= Time.deltaTime;
        }

        // Check the distance to the player, if it's too close, find a place to teleport to and go there.
        // Also ensure that a teleport isn't already occuring, if this isn't checked the entire screen will be populated with
        // teleportation animations
        if (Vector3.Distance(transform.position, player.position) <= teleportRange && !teleporting)
        {
            // get the node for the teleport location
            Node teleportLocation = roomEvent.TeleportLocation();

            // Ensure that the location isn't null, and teleport to the location
            if (teleportLocation != null)
            {
                // set the status to teleporting and the location to the more public variable
                teleporting = true;
                if (!hasTeleported)
                {
                    hasTeleported = true;
                }
                teleportNode = teleportLocation;

                // Play teleport animation at current position
                GameObject outTeleport = Instantiate(teleportAnimation, transform.position, Quaternion.identity);
                outTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                // Play teleport animation at new position
                GameObject inTeleport = Instantiate(teleportAnimation, teleportLocation.worldPosition, Quaternion.identity);
                inTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                // Move the enemy to the new position
                Teleport();

                // destroy the animation objects
                Destroy(outTeleport, 1.0f);
                Destroy(inTeleport, 1.0f);

                // reset the timer to teleport
                timeBetweenTeleport = Random.Range(5, 11);
            }
        }

        if (timeBetweenTeleport <= 0 && !teleporting && hasTeleported)
        {
            // check the comments above... it's virutally the same thing
            Node teleportLocation = roomEvent.TeleportLocation();

            if (teleportLocation != null)
            {
                teleporting  = true;
                teleportNode = teleportLocation;

                GameObject outTeleport = Instantiate(teleportAnimation, transform.position, Quaternion.identity);
                outTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                GameObject inTeleport = Instantiate(teleportAnimation, teleportLocation.worldPosition, Quaternion.identity);
                inTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                Teleport();

                Destroy(outTeleport, 1.0f);
                Destroy(inTeleport, 1.0f);

                timeBetweenTeleport = Random.Range(5, 11);
            }
        }
        else if (startTimeToTeleport <= 0 && !teleporting && !hasTeleported)
        {
            // check the comments above, it's virtually the same thing
            hasTeleported = true;
            Node teleportLocation = roomEvent.TeleportLocation();

            if (teleportLocation != null)
            {
                teleporting  = true;
                teleportNode = teleportLocation;

                GameObject outTeleport = Instantiate(teleportAnimation, transform.position, Quaternion.identity);
                outTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                GameObject inTeleport = Instantiate(teleportAnimation, teleportLocation.worldPosition, Quaternion.identity);
                inTeleport.GetComponent <Animator>().SetTrigger("AnimationTrigger");

                Teleport();

                Destroy(outTeleport, 1.0f);
                Destroy(inTeleport, 1.0f);

                timeBetweenTeleport = Random.Range(5, 11);
            }
        }
        else
        {
            timeBetweenTeleport -= Time.deltaTime;
        }
    }