Advertisement
CassataGames

Collision Tester

Sep 16th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.45 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.     public bool DebugNull;
  20.  
  21.     public bool PotentialNull;
  22.     public bool CheckedForNull;
  23.  
  24.     // Since colliders may rely on an initial setting,
  25.     // this allows us to communicate when we've settled in.
  26.     public bool Initialized;
  27.     private float InitializeTimer;
  28.     private float InitializeClock = 0.5f;
  29.  
  30.     // Check to see if we're touching anything
  31.     public bool HittingAnything;
  32.  
  33.     // If we don't need to check for direction, save ourselves the effort and don't.
  34.     public bool CheckDirections;
  35.  
  36.     // This is ONLY for testing with the raycast. Layer chart is used for Triggers.
  37.     public LayerMask RaycastMask;
  38.  
  39.     // If layers are being used properly, this should be all we need the majority of the time. :)
  40.     public bool HitTop;
  41.     public bool HitBottom;
  42.     public bool HitLeft;
  43.     public bool HitRight;
  44.  
  45.     // The list of colliders and their information.
  46.     public List<ColliderDetected> ColliderDetectList = new List<ColliderDetected>();
  47.     public ColliderDetected Placeholder;
  48.  
  49.     public string[] ExcludedNames;
  50.     public string[] ExcludedTags;
  51.  
  52.     void Start()
  53.     {
  54.         // Make sure the list is completely clean before using it.
  55.         ColliderDetectList.Clear();        
  56.     }
  57.    
  58.     public bool CheckExcludedNames(Collider InDetected)
  59.     {
  60.         bool found = false;
  61.         foreach (string Individual in ExcludedNames)
  62.         {
  63.             if (Individual == InDetected.name)
  64.                 found = true;
  65.         }
  66.         return found;
  67.     }
  68.     public bool CheckExcludedTags(Collider InDetected)
  69.     {
  70.         bool found = false;
  71.         foreach (string Individual in ExcludedTags)
  72.         {
  73.             if (Individual == InDetected.tag)
  74.                 found = true;
  75.         }
  76.         return found;
  77.     }
  78.  
  79.     public void RemoveDuplicates()
  80.     {
  81.         int index = 0;
  82.         ColliderDetected Previous = Placeholder;
  83.         foreach (ColliderDetected Individual in ColliderDetectList)
  84.         {
  85.             //Debug.Log(Individual + " & " + Previous);
  86.             if (Individual.collider == Previous.collider)
  87.             {
  88.                 ColliderDetectList.RemoveAt(index);
  89.                 break;
  90.             }
  91.             Previous = Individual;
  92.             index++;
  93.         }
  94.     }
  95.  
  96.     // Add collider information to the CollisionList;
  97.     void OnTriggerEnter(Collider collider)
  98.     {
  99.         if (DebugCollisionTests) // Marks in the debugger when collisions enter.
  100.             Debug.Log("[  ] - OnTriggerEnter: " + gameObject.collider + " has entered " + collider.name);
  101.  
  102.         // Add the new detected collider to our list of colliders as long as it's not in the excluded name or tag list.
  103.         // Adding the current collider allows for you to detect your distance and location from the object.        
  104.         if (!CheckExcludedNames(collider) && !CheckExcludedTags(collider))
  105.             ColliderDetectList.Add(new ColliderDetected(collider, gameObject.collider));
  106.        
  107.         // If we enter a trigger, update our collisions.
  108.         if (CheckDirections)
  109.             RaycastTest();
  110.     }
  111.  
  112.     // Remove collider information from the CollisionList;
  113.     void OnTriggerExit(Collider collider)
  114.     {
  115.         if (DebugCollisionTests) // Marks in the debugger when collisions exit.
  116.             Debug.Log("[X] - OnTriggerExit: " + gameObject.collider + " has exited " + collider.name);
  117.  
  118.         // listCount allows us to go through each collider in the list so we know which one to remove when we find a match.
  119.         int indexCount = 0;
  120.        
  121.         // Go through each colliderDetected in the ColliderDetectList to find a matching collider.
  122.         // DO NOT IDENTIFY BY NAME.
  123.         // COLLIDERS ARE ALWAYS UNIQUE.
  124.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  125.         {
  126.             if (colliderDetected.collider == null & DebugCollisionTests)
  127.             {
  128.                 print("Discovered a null!");
  129.             }
  130.  
  131.             // Check each collider
  132.             if (colliderDetected.collider == collider)
  133.             {
  134.                 // Remove a collider based off of it's index count number.
  135.                 ColliderDetectList.RemoveAt(indexCount);
  136.                 // Break away from foreach loop so it doesn't try to finish the list after it's been changed.
  137.                 break;
  138.             }
  139.             // Adjust the index count if we haven't found a match yet.
  140.             // This is important so that once we do find a match, we can remove it.
  141.             indexCount++;
  142.         }
  143.  
  144.         // We exited a trigger, update our collisions.
  145.         if (CheckDirections)
  146.             RaycastTest();
  147.     }
  148.  
  149.     void RaycastTest()
  150.     {
  151.         HitTop = false;
  152.         HitBottom = false;
  153.         HitLeft = false;
  154.         HitRight = false;
  155.  
  156.         // Easy access to Ray directions.
  157.         Ray Up = new Ray(transform.position, Vector3.up);
  158.         Ray Down = new Ray(transform.position, Vector3.down);
  159.         Ray Left = new Ray(transform.position, Vector3.right); // Don't change. Properly inversed.
  160.         Ray Right = new Ray(transform.position, Vector3.left); // Don't change. Properly inversed.
  161.  
  162.         RaycastHit hit;
  163.  
  164.         // Check all four directions for a hit.
  165.         if (Physics.Raycast(Up, out hit, transform.localScale.y / 2f, RaycastMask))
  166.             HitTop = true;
  167.         if (Physics.Raycast(Down, out hit, transform.localScale.y / 2f, RaycastMask))
  168.             HitBottom = true;
  169.         if (Physics.Raycast(Left, out hit, transform.localScale.x / 2f, RaycastMask))
  170.             HitLeft = true;
  171.         if (Physics.Raycast(Right, out hit, transform.localScale.x / 2f, RaycastMask))
  172.             HitRight = true;
  173.     }
  174.  
  175.     public DisplayDebug DebugText;
  176.  
  177.     void FixedUpdate() // REQUIRED TO BE FIXED UPDATE TO FUNCTION PROPERLY - DO NOT CHANGE //
  178.     {
  179.         if (!Initialized)
  180.         {
  181.             InitializeTimer = InitializeTimer + (1 * Time.deltaTime);
  182.             if (InitializeTimer > InitializeClock)
  183.                 Initialized = true;
  184.         }
  185.  
  186.         if (ColliderDetectList.Count > 0)
  187.             HittingAnything = true;            
  188.         else
  189.             HittingAnything = false;
  190.  
  191.         if (DebugText != null)
  192.             if (ColliderDetectList.Count > 0)
  193.                 DebugText.DisplayDebugText(name + ": " + HittingAnything + "; " + ColliderDetectList[0].collider);
  194.             else
  195.                 DebugText.DisplayDebugText(name + ": " + HittingAnything);
  196.  
  197.         CheckedForNull = false;
  198.         if (PotentialNull)
  199.         {
  200.             int indexCount = 0;
  201.             foreach (ColliderDetected colliderDetected in ColliderDetectList)
  202.             {
  203.                 if (colliderDetected.collider == null)
  204.                 {
  205.                     ColliderDetectList.RemoveAt(indexCount);
  206.                     if (DebugNull)
  207.                         print("Null removed.");
  208.                     break;
  209.                 }
  210.                 if (!colliderDetected.collider.gameObject.activeSelf)
  211.                 {
  212.                     ColliderDetectList.RemoveAt(indexCount);
  213.                     if (DebugNull)
  214.                         print("Inactive removed.");
  215.                     break;
  216.                 }
  217.                 indexCount++;
  218.             }
  219.             CheckedForNull = true;
  220.         }
  221.         //Debug.Log(ColliderDetectList.Capacity);
  222.         RemoveDuplicates();
  223.         //CheckForActive();
  224.     }
  225.  
  226.     public void ClearAllExcept(string name)
  227.     {
  228.         int indexCount = 0;
  229.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  230.         {
  231.             if (colliderDetected.name != name)
  232.             {
  233.                 if (colliderDetected.tag != "EdgeDetect")
  234.                 {
  235.                     ColliderDetectList.RemoveAt(indexCount);
  236.                     if (DebugNull)
  237.                         print("Removed " + colliderDetected.name);
  238.                     break;
  239.                 }
  240.             }
  241.         }
  242.     }
  243.     public void ClearAllExcept(string name1, string name2)
  244.     {
  245.         int indexCount = 0;
  246.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  247.         {
  248.             if (colliderDetected.name != name1 && colliderDetected.name != name2)
  249.             {
  250.                 if (colliderDetected.tag != "EdgeDetect")
  251.                 {
  252.                     ColliderDetectList.RemoveAt(indexCount);
  253.                     if (DebugNull)
  254.                         print("Removed " + colliderDetected.name);
  255.                     break;
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261.     public void RemoveName(string name)
  262.     {
  263.         int indexCount = 0;
  264.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  265.         {
  266.             if (colliderDetected.name == name)
  267.             {
  268.                 ColliderDetectList.RemoveAt(indexCount);
  269.                 if (DebugNull)
  270.                     print("Removed " + colliderDetected.name);
  271.                 break;
  272.             }
  273.         }
  274.     }
  275.  
  276.  
  277.     public void RemoveTag(string tag)
  278.     {
  279.         int indexCount = 0;
  280.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  281.         {
  282.             if (colliderDetected.tag == tag)
  283.             {
  284.                 ColliderDetectList.RemoveAt(indexCount);
  285.                 if (DebugNull)
  286.                     print("Removed " + tag);
  287.                 break;
  288.             }
  289.         }
  290.     }
  291.  
  292.     public bool CheckForTag(string tag)
  293.     {
  294.         bool matchFound = false;
  295.         if (HittingAnything)
  296.         {
  297.             foreach (ColliderDetected colliderDetected in ColliderDetectList)
  298.             {
  299.                 if (colliderDetected.collider == null)
  300.                     break;
  301.                 if (colliderDetected.collider.tag == tag)
  302.                 {
  303.                     matchFound = true;
  304.                 }
  305.             }
  306.         }
  307.         return matchFound;
  308.     }
  309.     public bool CheckForName(string name)
  310.     {
  311.         bool matchFound = false;
  312.         if (HittingAnything)
  313.         {
  314.             foreach (ColliderDetected colliderDetected in ColliderDetectList)
  315.             {
  316.                 if (colliderDetected.collider == null)
  317.                     break;
  318.                 if (colliderDetected.collider.name == name)
  319.                 {
  320.                     matchFound = true;
  321.                 }
  322.             }
  323.         }
  324.         return matchFound;
  325.     }
  326.     public void CheckForActive()
  327.     {
  328.         int indexCount = 0;
  329.         foreach (ColliderDetected individual in ColliderDetectList)
  330.         {
  331.             if (!individual.collider.enabled)
  332.             {
  333.                 Debug.Log("Removed inactive.");
  334.                 ColliderDetectList.RemoveAt(indexCount);
  335.                 break;
  336.             }
  337.         }
  338.  
  339.     }
  340.  
  341.     // This class holds information for each collider detected.
  342.     // Any information about the collision can be found here.
  343.     [System.Serializable]
  344.     public class ColliderDetected
  345.     {
  346.         public string name;         // Listed for easy debugging.
  347.         public string tag;          // Listed for easy debugging.
  348.         public Collider collider;    // Required.
  349.  
  350.         // Constructor for creating a new ColliderDetected.
  351.         public ColliderDetected(Collider newCollider, Collider selfCollider)
  352.         {
  353.             name = newCollider.name;
  354.             tag = newCollider.tag;
  355.             collider = newCollider;
  356.         }
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement