Advertisement
CassataGames

Collider Script

Mar 23rd, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.27 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.     // Since colliders may rely on an initial setting,
  21.     // this allows us to communicate when we've settled in.
  22.     public bool Initialized;
  23.     private float InitializeTimer;
  24.     private float InitializeClock = 0.5f;
  25.  
  26.     // Check to see if we're touching anything
  27.     public bool HittingAnything;
  28.  
  29.     // If we don't need to check for direction, save ourselves the effort and don't.
  30.     public bool CheckDirections;
  31.  
  32.     // This is ONLY for testing with the raycast. Layer chart is used for Triggers.
  33.     public LayerMask RaycastMask;
  34.  
  35.     // If layers are being used properly, this should be all we need the majority of the time. :)
  36.     public bool HitTop;
  37.     public bool HitBottom;
  38.     public bool HitLeft;
  39.     public bool HitRight;
  40.  
  41.     // The list of colliders and their information.
  42.     public List<ColliderDetected> ColliderDetectList = new List<ColliderDetected>();
  43.  
  44.     void Start()
  45.     {
  46.         // Make sure the list is completely clean before using it.
  47.         ColliderDetectList.Clear();        
  48.     }
  49.    
  50.     // Add collider information to the CollisionList;
  51.     void OnTriggerEnter(Collider collider)
  52.     {
  53.         if (DebugCollisionTests) // Marks in the debugger when collisions enter.
  54.             Debug.Log("[  ] - OnTriggerEnter: " + gameObject.collider + " has entered " + collider.name);
  55.  
  56.         // Add the new detected collider to our list of colliders.
  57.         // Adding the current collider allows for you to detect your distance and location from the object.
  58.         ColliderDetectList.Add(new ColliderDetected(collider, gameObject.collider));
  59.        
  60.         // If we enter a trigger, update our collisions.
  61.         if (CheckDirections)
  62.             RaycastTest();
  63.     }
  64.  
  65.     // Remove collider information from the CollisionList;
  66.     void OnTriggerExit(Collider collider)
  67.     {
  68.         if (DebugCollisionTests) // Marks in the debugger when collisions exit.
  69.             Debug.Log("[X] - OnTriggerExit: " + gameObject.collider + " has exited " + collider.name);
  70.  
  71.         // listCount allows us to go through each collider in the list so we know which one to remove when we find a match.
  72.         int indexCount = 0;
  73.        
  74.         // Go through each colliderDetected in the ColliderDetectList to find a matching collider.
  75.         // DO NOT IDENTIFY BY NAME.
  76.         // COLLIDERS ARE ALWEAYS UNIQUE.
  77.         foreach (ColliderDetected colliderDetected in ColliderDetectList)
  78.         {
  79.             // Check each collider
  80.             if (colliderDetected.collider == collider)
  81.             {
  82.                 // Remove a collider based off of it's index count number.
  83.                 ColliderDetectList.RemoveAt(indexCount);
  84.                 // Break away from foreach loop so it doesn't try to finish the list after it's been changed.
  85.                 break;
  86.             }
  87.             // Adjust the index count if we haven't found a match yet.
  88.             // This is important so that once we do find a match, we can remove it.
  89.             indexCount++;
  90.         }
  91.  
  92.         // We exited a trigger, update our collisions.
  93.         if (CheckDirections)
  94.             RaycastTest();
  95.     }
  96.  
  97.     void RaycastTest()
  98.     {
  99.         HitTop = false;
  100.         HitBottom = false;
  101.         HitLeft = false;
  102.         HitRight = false;
  103.  
  104.         // Easy access to Ray directions.
  105.         Ray Up = new Ray(transform.position, Vector3.up);
  106.         Ray Down = new Ray(transform.position, Vector3.down);
  107.         Ray Left = new Ray(transform.position, Vector3.right); // Don't change. Properly inversed.
  108.         Ray Right = new Ray(transform.position, Vector3.left); // Don't change. Properly inversed.
  109.  
  110.         RaycastHit hit;
  111.  
  112.         // Check all four directions for a hit.
  113.         if (Physics.Raycast(Up, out hit, transform.localScale.y / 2f, RaycastMask))
  114.             HitTop = true;
  115.         if (Physics.Raycast(Down, out hit, transform.localScale.y / 2f, RaycastMask))
  116.             HitBottom = true;
  117.         if (Physics.Raycast(Left, out hit, transform.localScale.x / 2f, RaycastMask))
  118.             HitLeft = true;
  119.         if (Physics.Raycast(Right, out hit, transform.localScale.x / 2f, RaycastMask))
  120.             HitRight = true;
  121.     }
  122.  
  123.     void Update()
  124.     {
  125.         if (!Initialized)
  126.         {
  127.             InitializeTimer = InitializeTimer + (1 * Time.deltaTime);
  128.             if (InitializeTimer > InitializeClock)
  129.                 Initialized = true;
  130.         }
  131.  
  132.         if (ColliderDetectList.Count > 0)
  133.             HittingAnything = true;
  134.         else
  135.             HittingAnything = false;
  136.     }
  137.  
  138.     // This class holds information for each collider detected.
  139.     // Any information about the collision can be found here.
  140.     [System.Serializable]
  141.     public class ColliderDetected
  142.     {
  143.         private string name;         // Listed for easy debugging.
  144.         private string tag;          // Listed for easy debugging.
  145.         public Collider collider;    // Required.
  146.  
  147.         // Constructor for creating a new ColliderDetected.
  148.         public ColliderDetected(Collider newCollider, Collider selfCollider)
  149.         {
  150.             name = newCollider.name;
  151.             tag = newCollider.tag;
  152.             collider = newCollider;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement