Exemplo n.º 1
0
    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("bounds"))
        {
            Vector3 bounds   = other.GetComponent <Collider>().bounds.size;
            Vector3 otherPos = other.transform.position;

            bool wrapped = false;

            //If exit right
            if (transform.position.x >
                otherPos.x + (bounds.x / 2))
            {
                transform.position =
                    new Vector3(otherPos.x - (bounds.x / 2),
                                transform.position.y,
                                transform.position.z);
                wrapped = true;
            }
            else
            {
                //if exit left
                if (transform.position.x <
                    otherPos.x - (bounds.x / 2))
                {
                    transform.position =
                        new Vector3(otherPos.x + (bounds.x / 2),
                                    transform.position.y,
                                    transform.position.z);
                    wrapped = true;
                }
            }

            if (transform.position.z >
                otherPos.z + (bounds.z / 2))
            {
                //if exit forward
                transform.position =
                    new Vector3(
                        transform.position.x,
                        transform.position.y,
                        otherPos.z - (bounds.z / 2));
                wrapped = true;
            }
            else
            {
                //if exit backwards
                if (transform.position.z <
                    otherPos.z - (bounds.z / 2))
                {
                    transform.position =
                        new Vector3(
                            transform.position.x,
                            transform.position.y,
                            otherPos.z + (bounds.z / 2));
                    wrapped = true;
                }
            }

            //if exited bounds but none of the above
            //(they steer up and down to avoid bottom and top, but just in case)
            if (!wrapped)
            {
                Transform sP = BoidsManager.GetRandomSpawnPoint();

                //Respawn
                this.transform.position = sP.position;
                this.transform.rotation = sP.rotation;
#if DEBUG
                OobCount++;
                Debug.LogWarning(
                    "Out of bound count: " + OobCount +
                    " - total time: " + Time.time, gameObject);
#endif
            }


            //invoke out of bounds event if not null
            onOutOfBounds?.Invoke();
        }
    }