Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ColliderTester : MonoBehaviour
- {
- // _________________________________________________________________________________
- // | !! IMPORTANT !! |
- // | + Make sure you assign a rigid body component to the object with this script. |
- // | + Make sure you enable "Is Trigger" to the collider component. |
- // |_______________________________________________________________________________|
- // This script is used to create an easy to identify list of objects the object's collider is currently in contact with.*
- // * To exclude objects from detection, assign them to layers and use the Physics menu to choose which layers intact with each other.
- // ** Be sure to check both sides of the layer chart as one layer can still be interacting with another from the other way around.
- // Performance low? Check how many OnTrigger calls this collider is making by enabling this.
- public bool DebugCollisionTests;
- public bool DebugNull;
- public bool PotentialNull;
- public bool CheckedForNull;
- // Since colliders may rely on an initial setting,
- // this allows us to communicate when we've settled in.
- public bool Initialized;
- private float InitializeTimer;
- private float InitializeClock = 0.5f;
- // Check to see if we're touching anything
- public bool HittingAnything;
- // If we don't need to check for direction, save ourselves the effort and don't.
- public bool CheckDirections;
- // This is ONLY for testing with the raycast. Layer chart is used for Triggers.
- public LayerMask RaycastMask;
- // If layers are being used properly, this should be all we need the majority of the time. :)
- public bool HitTop;
- public bool HitBottom;
- public bool HitLeft;
- public bool HitRight;
- // The list of colliders and their information.
- public List<ColliderDetected> ColliderDetectList = new List<ColliderDetected>();
- public ColliderDetected Placeholder;
- public string[] ExcludedNames;
- public string[] ExcludedTags;
- void Start()
- {
- // Make sure the list is completely clean before using it.
- ColliderDetectList.Clear();
- }
- public bool CheckExcludedNames(Collider InDetected)
- {
- bool found = false;
- foreach (string Individual in ExcludedNames)
- {
- if (Individual == InDetected.name)
- found = true;
- }
- return found;
- }
- public bool CheckExcludedTags(Collider InDetected)
- {
- bool found = false;
- foreach (string Individual in ExcludedTags)
- {
- if (Individual == InDetected.tag)
- found = true;
- }
- return found;
- }
- public void RemoveDuplicates()
- {
- int index = 0;
- ColliderDetected Previous = Placeholder;
- foreach (ColliderDetected Individual in ColliderDetectList)
- {
- //Debug.Log(Individual + " & " + Previous);
- if (Individual.collider == Previous.collider)
- {
- ColliderDetectList.RemoveAt(index);
- break;
- }
- Previous = Individual;
- index++;
- }
- }
- // Add collider information to the CollisionList;
- void OnTriggerEnter(Collider collider)
- {
- if (DebugCollisionTests) // Marks in the debugger when collisions enter.
- Debug.Log("[ ] - OnTriggerEnter: " + gameObject.collider + " has entered " + collider.name);
- // Add the new detected collider to our list of colliders as long as it's not in the excluded name or tag list.
- // Adding the current collider allows for you to detect your distance and location from the object.
- if (!CheckExcludedNames(collider) && !CheckExcludedTags(collider))
- ColliderDetectList.Add(new ColliderDetected(collider, gameObject.collider));
- // If we enter a trigger, update our collisions.
- if (CheckDirections)
- RaycastTest();
- }
- // Remove collider information from the CollisionList;
- void OnTriggerExit(Collider collider)
- {
- if (DebugCollisionTests) // Marks in the debugger when collisions exit.
- Debug.Log("[X] - OnTriggerExit: " + gameObject.collider + " has exited " + collider.name);
- // listCount allows us to go through each collider in the list so we know which one to remove when we find a match.
- int indexCount = 0;
- // Go through each colliderDetected in the ColliderDetectList to find a matching collider.
- // DO NOT IDENTIFY BY NAME.
- // COLLIDERS ARE ALWAYS UNIQUE.
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.collider == null & DebugCollisionTests)
- {
- print("Discovered a null!");
- }
- // Check each collider
- if (colliderDetected.collider == collider)
- {
- // Remove a collider based off of it's index count number.
- ColliderDetectList.RemoveAt(indexCount);
- // Break away from foreach loop so it doesn't try to finish the list after it's been changed.
- break;
- }
- // Adjust the index count if we haven't found a match yet.
- // This is important so that once we do find a match, we can remove it.
- indexCount++;
- }
- // We exited a trigger, update our collisions.
- if (CheckDirections)
- RaycastTest();
- }
- void RaycastTest()
- {
- HitTop = false;
- HitBottom = false;
- HitLeft = false;
- HitRight = false;
- // Easy access to Ray directions.
- Ray Up = new Ray(transform.position, Vector3.up);
- Ray Down = new Ray(transform.position, Vector3.down);
- Ray Left = new Ray(transform.position, Vector3.right); // Don't change. Properly inversed.
- Ray Right = new Ray(transform.position, Vector3.left); // Don't change. Properly inversed.
- RaycastHit hit;
- // Check all four directions for a hit.
- if (Physics.Raycast(Up, out hit, transform.localScale.y / 2f, RaycastMask))
- HitTop = true;
- if (Physics.Raycast(Down, out hit, transform.localScale.y / 2f, RaycastMask))
- HitBottom = true;
- if (Physics.Raycast(Left, out hit, transform.localScale.x / 2f, RaycastMask))
- HitLeft = true;
- if (Physics.Raycast(Right, out hit, transform.localScale.x / 2f, RaycastMask))
- HitRight = true;
- }
- public DisplayDebug DebugText;
- void FixedUpdate() // REQUIRED TO BE FIXED UPDATE TO FUNCTION PROPERLY - DO NOT CHANGE //
- {
- if (!Initialized)
- {
- InitializeTimer = InitializeTimer + (1 * Time.deltaTime);
- if (InitializeTimer > InitializeClock)
- Initialized = true;
- }
- if (ColliderDetectList.Count > 0)
- HittingAnything = true;
- else
- HittingAnything = false;
- if (DebugText != null)
- if (ColliderDetectList.Count > 0)
- DebugText.DisplayDebugText(name + ": " + HittingAnything + "; " + ColliderDetectList[0].collider);
- else
- DebugText.DisplayDebugText(name + ": " + HittingAnything);
- CheckedForNull = false;
- if (PotentialNull)
- {
- int indexCount = 0;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.collider == null)
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Null removed.");
- break;
- }
- if (!colliderDetected.collider.gameObject.activeSelf)
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Inactive removed.");
- break;
- }
- indexCount++;
- }
- CheckedForNull = true;
- }
- //Debug.Log(ColliderDetectList.Capacity);
- RemoveDuplicates();
- //CheckForActive();
- }
- public void ClearAllExcept(string name)
- {
- int indexCount = 0;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.name != name)
- {
- if (colliderDetected.tag != "EdgeDetect")
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Removed " + colliderDetected.name);
- break;
- }
- }
- }
- }
- public void ClearAllExcept(string name1, string name2)
- {
- int indexCount = 0;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.name != name1 && colliderDetected.name != name2)
- {
- if (colliderDetected.tag != "EdgeDetect")
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Removed " + colliderDetected.name);
- break;
- }
- }
- }
- }
- public void RemoveName(string name)
- {
- int indexCount = 0;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.name == name)
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Removed " + colliderDetected.name);
- break;
- }
- }
- }
- public void RemoveTag(string tag)
- {
- int indexCount = 0;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.tag == tag)
- {
- ColliderDetectList.RemoveAt(indexCount);
- if (DebugNull)
- print("Removed " + tag);
- break;
- }
- }
- }
- public bool CheckForTag(string tag)
- {
- bool matchFound = false;
- if (HittingAnything)
- {
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.collider == null)
- break;
- if (colliderDetected.collider.tag == tag)
- {
- matchFound = true;
- }
- }
- }
- return matchFound;
- }
- public bool CheckForName(string name)
- {
- bool matchFound = false;
- if (HittingAnything)
- {
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.collider == null)
- break;
- if (colliderDetected.collider.name == name)
- {
- matchFound = true;
- }
- }
- }
- return matchFound;
- }
- public void CheckForActive()
- {
- int indexCount = 0;
- foreach (ColliderDetected individual in ColliderDetectList)
- {
- if (!individual.collider.enabled)
- {
- Debug.Log("Removed inactive.");
- ColliderDetectList.RemoveAt(indexCount);
- break;
- }
- }
- }
- // This class holds information for each collider detected.
- // Any information about the collision can be found here.
- [System.Serializable]
- public class ColliderDetected
- {
- public string name; // Listed for easy debugging.
- public string tag; // Listed for easy debugging.
- public Collider collider; // Required.
- // Constructor for creating a new ColliderDetected.
- public ColliderDetected(Collider newCollider, Collider selfCollider)
- {
- name = newCollider.name;
- tag = newCollider.tag;
- collider = newCollider;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement