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;
- // This is ONLY for testing with the raycast. Layer chart is used for Triggers.
- public LayerMask RaycastTest;
- // 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(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.name,collider,gameObject.collider, RaycastTest));
- }
- // Remove collider information from the CollisionList;
- void OnTriggerExit(Collider collider)
- {
- if (DebugCollisionTests) // Marks in the debugger when collisions exit.
- Debug.Log(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++;
- }
- }
- void Update()
- {
- HitTop = false;
- HitBottom = false;
- HitLeft = false;
- HitRight = false;
- foreach (ColliderDetected colliderDetected in ColliderDetectList)
- {
- if (colliderDetected.HitTop)
- HitTop = true;
- if (colliderDetected.HitBottom)
- HitBottom = true;
- if (colliderDetected.HitLeft)
- HitLeft = true;
- if (colliderDetected.HitRight)
- HitRight = true;
- }
- }
- // 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.
- public float distance = -1; // Listed for easy access;
- public bool HitTop; // Listed for easy access;
- public bool HitBottom; // Listed for easy access;
- public bool HitLeft; // Listed for easy access;
- public bool HitRight; // Listed for easy access;
- // Constructor for creating a new ColliderDetected.
- public ColliderDetected(string setName, Collider newCollider, Collider selfCollider, LayerMask mask)
- {
- name = setName;
- tag = newCollider.tag;
- collider = newCollider;
- distance = Vector3.Distance(newCollider.transform.position, selfCollider.transform.position);
- #region Raycast Tests
- // Easy access to Ray directions.
- Ray Up = new Ray(selfCollider.transform.position, Vector3.up);
- Ray Down = new Ray(selfCollider.transform.position, Vector3.down);
- Ray Left = new Ray(selfCollider.transform.position, Vector3.right); // Don't change. Properly inversed.
- Ray Right = new Ray(selfCollider.transform.position, Vector3.left); // Don't change. Properly inversed.
- Ray UpLeft = new Ray(selfCollider.transform.position, new Vector3(1, 1, 0)); // Don't change. Properly inversed.
- Ray UpRight = new Ray(selfCollider.transform.position, new Vector3(-1, 1, 0)); // Don't change. Properly inversed.
- Ray DownLeft = new Ray(selfCollider.transform.position, new Vector3(1, -1, 0)); // Don't change. Properly inversed.
- Ray DownRight = new Ray(selfCollider.transform.position, new Vector3(-1, -1, 0)); // Don't change. Properly inversed.
- RaycastHit hit;
- // Check all four directions for a hit.
- if (Physics.Raycast(Up, out hit, distance, mask))
- HitTop = hit.collider == collider;
- if (Physics.Raycast(Down, out hit, distance, mask))
- HitBottom = hit.collider == collider;
- if (Physics.Raycast(Left, out hit, distance, mask))
- HitLeft = hit.collider == collider;
- if (Physics.Raycast(Right, out hit, distance, mask))
- HitRight = hit.collider == collider;
- // If a hit wasn't detected in any of the four directions, check the corners.
- // Check Top/Left Corner
- if (!HitTop & !HitLeft)
- {
- if (Physics.Raycast(UpLeft, out hit, distance, mask))
- if (hit.collider == collider)
- {
- HitTop = true;
- HitLeft = true;
- }
- }
- // Check Top/Right Corner
- if (!HitTop & !HitRight)
- {
- if (Physics.Raycast(UpRight, out hit, distance, mask))
- if (hit.collider == collider)
- {
- HitTop = true;
- HitRight = true;
- }
- }
- // Check Bottom/Left Corner
- if (!HitBottom & !HitLeft)
- {
- if (Physics.Raycast(DownLeft, out hit, distance, mask))
- if (hit.collider == collider)
- {
- HitBottom = true;
- HitLeft = true;
- }
- }
- // Check Bottom/Right Corner
- if (!HitBottom & !HitRight)
- {
- if (Physics.Raycast(DownRight, out hit, distance, mask))
- if (hit.collider == collider)
- {
- HitBottom = true;
- HitRight = true;
- }
- }
- #endregion
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement