Advertisement
Ultroman

Draw unselected colliders in Unity

Feb 22nd, 2018
1,751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. /*
  2.  * Written by Jonas Tingmose aka. Ultroman the Tacoman
  3.  * 23 Feb. 2018
  4.  *
  5.  * Attach this component to a GameObject, to draw all capsule, box and sphere colliders
  6.  * on that specific gameobject, with an option to include all colliders on its children.
  7.  * There is NO SUPPORT for wheel, spatial mapping, terrain or mesh colldiers.
  8.  * If you code support for these, please let me know, so I can update the script code,
  9.  * obviously credit you, and we can keep this useful tool available for all!
  10.  *
  11.  * NOTE!!! Requires the FREE Unity asset "Debug Drawing Extension" found here:
  12.  * https://assetstore.unity.com/packages/tools/debug-drawing-extension-11396
  13.  * Aside from the readme and example.cs, it's a single, static .cs file, which gives you
  14.  * the amazing power, to draw many shapes, like cylinders, circles, arrows, local cubes,
  15.  * cones etc., and of course capsules, which is what this DrawColliders script uses it for.
  16.  */
  17.  
  18. using Assets.Utils.DebugDrawingExtension;
  19. using UnityEngine;
  20.  
  21. [ExecuteInEditMode]
  22. public class DrawColliders : MonoBehaviour {
  23.     public bool AlwaysShowCollider = true;
  24.     public bool IncludeCollidersInChildren = true;
  25.     public Color GizmoColor = Color.red;
  26.     private Collider[] _colliders;
  27.     private BoxCollider _boxTemp;
  28.     private SphereCollider _sphereTemp;
  29.     private CapsuleCollider _capsuleTemp;
  30.     private Color _prevColor;
  31.    
  32.     void OnDrawGizmos()
  33.     {
  34.         _colliders = IncludeCollidersInChildren ? GetComponentsInChildren<Collider>() : GetComponents<Collider>();
  35.  
  36.         if (_colliders == null || _colliders.Length == 0)
  37.             return;
  38.  
  39.         if (AlwaysShowCollider)
  40.         {
  41.             _prevColor = Gizmos.color;
  42.             Gizmos.color = GizmoColor;
  43.  
  44.             Collider _temp;
  45.             for (int i = 0; i < _colliders.Length; i++)
  46.             {
  47.                 _temp = _colliders[i];
  48.                 if (!_temp.enabled)
  49.                     continue;
  50.                 if ((_boxTemp = _temp as BoxCollider) != null)
  51.                 {
  52.                     Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
  53.  
  54.                     Gizmos.matrix = Matrix4x4.TRS(_boxTemp.transform.TransformPoint(_boxTemp.center), _boxTemp.transform.rotation, _boxTemp.transform.lossyScale);
  55.                     Gizmos.DrawWireCube(Vector3.zero, _boxTemp.size);
  56.  
  57.                     Gizmos.matrix = oldGizmosMatrix;
  58.                 }
  59.                 else if((_sphereTemp = _temp as SphereCollider) != null)
  60.                 {
  61.                     Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
  62.  
  63.                     Gizmos.matrix = Matrix4x4.TRS(_sphereTemp.transform.TransformPoint(_sphereTemp.center), _sphereTemp.transform.rotation, Vector3.one *
  64.                         Mathf.Max(Mathf.Abs(_sphereTemp.transform.lossyScale.x), Mathf.Max(Mathf.Abs(_sphereTemp.transform.lossyScale.y), Mathf.Abs(_sphereTemp.transform.lossyScale.z))));
  65.                     Gizmos.DrawWireSphere(Vector3.zero, _sphereTemp.radius);
  66.  
  67.                     Gizmos.matrix = oldGizmosMatrix;
  68.                 }
  69.                 else if((_capsuleTemp = _temp as CapsuleCollider) != null)
  70.                 {
  71.                     Vector3 ls = _capsuleTemp.transform.lossyScale;
  72.                     var centerHalfScale = new Vector3(_capsuleTemp.center.x * ls.x, _capsuleTemp.center.y * ls.y, _capsuleTemp.center.z * ls.z);
  73.                     float halfHeight = _capsuleTemp.height * Mathf.Abs(ls.y) * 0.5f;
  74.                     var directionVector = _capsuleTemp.transform.up;
  75.  
  76.                     // direction == 0 is the X-axis
  77.                     if (_capsuleTemp.direction == 0)
  78.                         directionVector = (Quaternion.AngleAxis(90, Vector3.forward) * Vector3.up);
  79.                     // direction == 1 is the Y-axis
  80.                     else if (_capsuleTemp.direction == 1)
  81.                         directionVector = (Quaternion.AngleAxis(90, Vector3.up) * Vector3.up);
  82.                     // direction == 2 is the Z-axis
  83.                     else if (_capsuleTemp.direction == 2)
  84.                         directionVector = (Quaternion.AngleAxis(90, Vector3.right) * Vector3.up);
  85.  
  86.                     Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
  87.                     Gizmos.matrix = Matrix4x4.TRS(_capsuleTemp.transform.position, _capsuleTemp.transform.rotation, Vector3.one);
  88.                     Vector3 capsuleBoundsStart = centerHalfScale + directionVector * halfHeight;
  89.                     Vector3 capsuleBoundsEnd = centerHalfScale - directionVector * halfHeight;
  90.  
  91.                     DebugExtension.DrawCapsule(capsuleBoundsStart, capsuleBoundsEnd, GizmoColor,
  92.                         _capsuleTemp.radius * Mathf.Max(Mathf.Abs(ls.x), Mathf.Abs(ls.z)) // scale radius by: capsule radius, multiplied by the largest of X and Z lossyScales
  93.                         );
  94.  
  95.                     Gizmos.matrix = oldGizmosMatrix;
  96.                 }
  97.             }
  98.  
  99.             Gizmos.color = _prevColor;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement