Advertisement
Ultroman

Draw unselected colliders in Unity

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