Ejemplo n.º 1
0
        private void DoSubCollisionChecking(ref Vector3 currPos, ref Vector3 currSize, int collidersSize)
        {
            Logging.Log("[SUB COLLISION CHECK]: Started");

            // A separate loop that moves the object out of the colliding object

            var subCollisionCheckIterations = 0;

            while (true)
            {
                Logging.Log($"[SUB COLLISION CHECK]: Iteration {subCollisionCheckIterations} | Current position: {currPos.ToString("F4")} | Current Size: {currSize.ToString("F4")}");

                if (subCollisionCheckIterations++ > 100)
                {
                    Logging.Log("[SUB COLLISION CHECK]: Too many iterations");
                    break;
                }

                Collider[] newColliders = RayCaster.OverLapBox(currPos, currSize, Quaternion.identity);

                var forward = false;
                var left    = false;
                var back    = false;
                var right   = false;

                foreach (Collider newColl in newColliders)
                {
                    CheckCollidingDirections(ref currPos, newColl.transform.position, ref forward, ref left, ref back, ref right, newColl.gameObject, true, true);

                    MoveBasedOnCollisions(ref currPos, forward, left, back, right, "[SUB COLLISION CHECK:]");
                }
            }

            Logging.Log("[SUB COLLISION CHECK]: Ended");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns false if the object was found to be inside another object (and was therefore moved from inside this method)
        /// </summary>
        private bool GetCollisions(ref Vector3 currPos, ref Vector3 currSize, out bool collForward, out bool collLeft, out bool collBack, out bool collRight)
        {
            collForward = false;
            collLeft    = false;
            collBack    = false;
            collRight   = false;



            Collider[] colliders     = RayCaster.OverLapBox(currPos, currSize, Quaternion.identity);
            int        collidersSize = colliders.Length;

            if (collidersSize == 0)
            {
                currSize.x += boxCheckDistanceIncreasePerAttempt;
                currSize.z += boxCheckDistanceIncreasePerAttempt;
            }


            var collNames = "";

            foreach (Collider coll in colliders)
            {
                bool isNotInsideOtherObject = CheckCollidingDirections(ref currPos, coll, ref collForward, ref collLeft, ref collBack, ref collRight, false, true);

                if (!isNotInsideOtherObject)
                {
                    DoSubCollisionChecking(ref currPos, ref currSize, collidersSize);
                    return(false);
                }

                if (collNames != "")
                {
                    collNames += ",";
                }
                collNames += " " + coll.name;
            }

            Logging.Log($"Collided with {collNames}");

            return(true);
        }