Advertisement
teler1k

ColliderCreator.cs

Feb 22nd, 2021 (edited)
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. namespace Assets.Scripts.Utilities
  5. {
  6.     public class ColliderCreator : MonoBehaviour
  7.     {
  8.         [Header("Gizmos")]
  9.         public bool visualize = true;
  10.         public Color gizmosColor = Color.white;
  11.  
  12.         [Header("Generation")]
  13.         public bool generateCapsuleColliders = true;
  14.         public bool generateBoxColliders = true;
  15.  
  16. #if UNITY_EDITOR
  17.         private void OnDrawGizmos()
  18.         {
  19.             if (!visualize)
  20.                 return;
  21.  
  22.             // start at 1 (2nd child), first child is container for box colliders.
  23.             for (int i = 1; i < transform.childCount; i++)
  24.             {
  25.                 var target = transform.GetChild(i);
  26.  
  27.                 // render capsule for this child.
  28.                 if (generateCapsuleColliders)
  29.                 {
  30.                     DrawWireCapsule(target.position, target.rotation, target.localScale.x, target.localScale.y);
  31.                 }
  32.  
  33.                 // render box for child inbetween this and next child.
  34.                 if (generateBoxColliders && i < transform.childCount - 1)
  35.                 {
  36.                     var other = transform.GetChild(i + 1);
  37.  
  38.                     var position = (target.position + other.position) / 2f;
  39.                     var size = new Vector3((target.localScale.x + other.localScale.x) / 2f, (target.localScale.y + other.localScale.y) / 2f, Vector3.Distance(target.position, other.position));
  40.  
  41.                     Matrix4x4 rotationMatrix = Matrix4x4.TRS(position, Quaternion.LookRotation(other.position - target.position), size);
  42.                     Gizmos.matrix = rotationMatrix;
  43.  
  44.                     Gizmos.color = gizmosColor * 0.3f;
  45.                     Gizmos.DrawCube(Vector3.zero, Vector3.one);
  46.  
  47.                     Gizmos.color = gizmosColor;
  48.                     Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
  49.  
  50.                     Gizmos.matrix = Matrix4x4.identity;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         private void DrawWireCapsule(Vector3 position, Quaternion rotation, float radius, float height)
  56.         {
  57.             Handles.color = gizmosColor;
  58.             Matrix4x4 angleMatrix = Matrix4x4.TRS(position, rotation, Handles.matrix.lossyScale);
  59.             using (new Handles.DrawingScope(angleMatrix))
  60.             {
  61.                 var pointOffset = (height - (radius * 2)) / 2;
  62.  
  63.                 // draw sideways.
  64.                 Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.left, Vector3.back, -180, radius);
  65.                 Handles.DrawLine(new Vector3(0, pointOffset, -radius), new Vector3(0, -pointOffset, -radius));
  66.                 Handles.DrawLine(new Vector3(0, pointOffset, radius), new Vector3(0, -pointOffset, radius));
  67.                 Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.left, Vector3.back, 180, radius);
  68.                 // draw frontways.
  69.                 Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.back, Vector3.left, 180, radius);
  70.                 Handles.DrawLine(new Vector3(-radius, pointOffset, 0), new Vector3(-radius, -pointOffset, 0));
  71.                 Handles.DrawLine(new Vector3(radius, pointOffset, 0), new Vector3(radius, -pointOffset, 0));
  72.                 Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.back, Vector3.left, -180, radius);
  73.                 // draw center.
  74.                 Handles.DrawWireDisc(Vector3.up * pointOffset, Vector3.up, radius);
  75.                 Handles.DrawWireDisc(Vector3.down * pointOffset, Vector3.up, radius);
  76.  
  77.             }
  78.         }
  79.  #endif
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement