Advertisement
CassataGames

Untitled

Mar 22nd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.20 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ColliderTester : MonoBehaviour
  6. {
  7.     // _________________________________________________________________________________
  8.     // |                             !! IMPORTANT !!                                   |
  9.     // | + Make sure you assign a rigid body component to the object with this script. |
  10.     // | + Make sure you enable "Is Trigger" to the collider component.                |
  11.     // |_______________________________________________________________________________|
  12.  
  13.     // This script is used to create an easy to identify list of objects the object's collider is currently in contact with.*
  14.     // * To exclude objects from detection, assign them to layers and use the Physics menu to choose which layers intact with each other.
  15.     // ** Be sure to check both sides of the layer chart as one layer can still be interacting with another from the other way around.
  16.  
  17.     // Performance low? Check how many OnTrigger calls this collider is making by enabling this.
  18.     public bool DebugCollisionTests;
  19.  
  20.     // This is ONLY for testing with the raycast. Layer chart is used for Triggers.
  21.     public LayerMask RaycastTest;
  22.  
  23.     // If layers are being used properly, this should be all we need the majority of the time. :)
  24.     public bool HitTop;
  25.     public bool HitBottom;
  26.     public bool HitLeft;
  27.     public bool HitRight;
  28.  
  29.     // The list of colliders and their information.
  30.     public List<ColliderDetected> ColliderDetectList = new List<ColliderDetected>();
  31.  
  32.     void Start()
  33.     {
  34.         // Make sure the list is completely clean before using it.
  35.         ColliderDetectList.Clear();
  36.     }
  37.  
  38.     // Add collider information to the CollisionList;
  39.     void OnTriggerEnter(Collider collider)
  40.     {
  41.         if (DebugCollisionTests) // Marks in the debugger when collisions enter.
  42.             Debug.Log(gameObject.collider + " has entered " + collider.name);
  43.  
  44.         // Add the new detected collider to our list of colliders.
  45.         // Adding the current collider allows for you to detect your distance and location from the object.
  46.         ColliderDetectList.Add(new ColliderDetected(collider.name,collider,gameObject.collider, RaycastTest));
  47.     }
  48.  
  49.     // Remove collider information from the CollisionList;
  50.     void OnTriggerExit(Collider collider)
  51.     {
  52.         if (DebugCollisionTests) // Marks in the debugger when collisions exit.
  53.             Debug.Log(gameObject.collider + " has exited " + collider.name);
  54.  
  55.         // listCount allows us to go through each collider in the list so we know which one to remove when we find a match.
  56.         int indexCount = 0;
  57.        
  58.         // Go through each colliderDetected in the ColliderDetectList to find a matching collider.
  59.         // DO NOT IDENTIFY BY NAME.
  60.         // COLLIDERS ARE ALWEAYS UNIQUE.
  61.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  62.         {
  63.             // Check each collider
  64.             if (colliderDetected.collider == collider)
  65.             {
  66.                 // Remove a collider based off of it's index count number.
  67.                 ColliderDetectList.RemoveAt(indexCount);
  68.                 // Break away from foreach loop so it doesn't try to finish the list after it's been changed.
  69.                 break;
  70.             }
  71.             // Adjust the index count if we haven't found a match yet.
  72.             // This is important so that once we do find a match, we can remove it.
  73.             indexCount++;
  74.         }
  75.     }
  76.  
  77.     void Update()
  78.     {
  79.         HitTop = false;
  80.         HitBottom = false;
  81.         HitLeft = false;
  82.         HitRight = false;
  83.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  84.         {
  85.             if (colliderDetected.HitTop)
  86.                 HitTop = true;
  87.             if (colliderDetected.HitBottom)
  88.                 HitBottom = true;
  89.             if (colliderDetected.HitLeft)
  90.                 HitLeft = true;
  91.             if (colliderDetected.HitRight)
  92.                 HitRight = true;
  93.         }
  94.     }
  95.  
  96.     // This class holds information for each collider detected.
  97.     // Any information about the collision can be found here.
  98.     [System.Serializable]
  99.     public class ColliderDetected
  100.     {
  101.         private string name;         // Listed for easy debugging.
  102.         private string tag;          // Listed for easy debugging.
  103.         public Collider collider;    // Required.
  104.         public float distance = -1; // Listed for easy access;
  105.  
  106.         public bool HitTop;         // Listed for easy access;
  107.         public bool HitBottom;      // Listed for easy access;
  108.         public bool HitLeft;        // Listed for easy access;
  109.         public bool HitRight;       // Listed for easy access;
  110.  
  111.         // Constructor for creating a new ColliderDetected.
  112.         public ColliderDetected(string setName, Collider newCollider, Collider selfCollider, LayerMask mask)
  113.         {
  114.             name = setName;
  115.             tag = newCollider.tag;
  116.             collider = newCollider;
  117.             distance = Vector3.Distance(newCollider.transform.position, selfCollider.transform.position);
  118.  
  119.             #region Raycast Tests
  120.            
  121.             // Easy access to Ray directions.
  122.             Ray Up = new Ray(selfCollider.transform.position, Vector3.up);
  123.             Ray Down = new Ray(selfCollider.transform.position, Vector3.down);
  124.             Ray Left = new Ray(selfCollider.transform.position, Vector3.right); // Don't change. Properly inversed.
  125.             Ray Right = new Ray(selfCollider.transform.position, Vector3.left); // Don't change. Properly inversed.
  126.  
  127.             Ray UpLeft = new Ray(selfCollider.transform.position, new Vector3(1, 1, 0)); // Don't change. Properly inversed.
  128.             Ray UpRight = new Ray(selfCollider.transform.position, new Vector3(-1, 1, 0)); // Don't change. Properly inversed.
  129.             Ray DownLeft = new Ray(selfCollider.transform.position, new Vector3(1, -1, 0)); // Don't change. Properly inversed.
  130.             Ray DownRight = new Ray(selfCollider.transform.position, new Vector3(-1, -1, 0)); // Don't change. Properly inversed.
  131.            
  132.             RaycastHit hit;
  133.  
  134.             // Check all four directions for a hit.
  135.             if (Physics.Raycast(Up, out hit, distance, mask))
  136.                 HitTop = hit.collider == collider;
  137.             if (Physics.Raycast(Down, out hit, distance, mask))
  138.                 HitBottom = hit.collider == collider;
  139.             if (Physics.Raycast(Left, out hit, distance, mask))
  140.                 HitLeft = hit.collider == collider;
  141.             if (Physics.Raycast(Right, out hit, distance, mask))
  142.                 HitRight = hit.collider == collider;
  143.  
  144.             // If a hit wasn't detected in any of the four directions, check the corners.
  145.             // Check Top/Left Corner
  146.             if (!HitTop & !HitLeft)
  147.             {
  148.                 if (Physics.Raycast(UpLeft, out hit, distance, mask))
  149.                     if (hit.collider == collider)
  150.                     {
  151.                         HitTop = true;
  152.                         HitLeft = true;
  153.                     }
  154.             }
  155.             // Check Top/Right Corner
  156.             if (!HitTop & !HitRight)
  157.             {
  158.                 if (Physics.Raycast(UpRight, out hit, distance, mask))
  159.                     if (hit.collider == collider)
  160.                     {
  161.                         HitTop = true;
  162.                         HitRight = true;
  163.                     }
  164.             }
  165.             // Check Bottom/Left Corner
  166.             if (!HitBottom & !HitLeft)
  167.             {
  168.                 if (Physics.Raycast(DownLeft, out hit, distance, mask))
  169.                     if (hit.collider == collider)
  170.                     {
  171.                         HitBottom = true;
  172.                         HitLeft = true;
  173.                     }
  174.             }
  175.             // Check Bottom/Right Corner
  176.             if (!HitBottom & !HitRight)
  177.             {
  178.                 if (Physics.Raycast(DownRight, out hit, distance, mask))
  179.                     if (hit.collider == collider)
  180.                     {
  181.                         HitBottom = true;
  182.                         HitRight = true;
  183.                     }
  184.             }
  185.             #endregion
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement