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;
- // 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>();
- void Start()
- {
- // Make sure the list is completely clean before using it.
- ColliderDetectList.Clear();
- }
- // 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.
- // Adding the current collider allows for you to detect your distance and location from the object.
- 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 ALWEAYS UNIQUE.
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- // 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;
- }
- void Update()
- {
- if (!Initialized)
- {
- InitializeTimer = InitializeTimer + (1 * Time.deltaTime);
- if (InitializeTimer > InitializeClock)
- Initialized = true;
- }
- if (ColliderDetectList.Count > 0)
- HittingAnything = true;
- else
- HittingAnything = false;
- }
- // This class holds information for each collider detected.
- // Any information about the collision can be found here.
- [System.Serializable]
- public class ColliderDetected
- {
- private string name; // Listed for easy debugging.
- private 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