Guest User

DrawSphereCast

a guest
Dec 3rd, 2017
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // Draw the capsule shape traversed by a spherecast.
  2.     void DrawSphereCast(Ray ray, float radius, float length, Color32 color, float duration) {
  3.         Matrix4x4 frame = Matrix4x4.TRS(
  4.             ray.origin,
  5.             Quaternion.LookRotation(ray.direction),
  6.             radius * Vector3.one);
  7.        
  8.         float scaledLength = length / radius;
  9.  
  10.         // Using a pythagorean triple here to pick intermediate points on the sphere.
  11.         float a = 33f / 65f;
  12.         float b = 56f / 65f;
  13.  
  14.         // Right side.
  15.         Debug.DrawLine(frame * new Vector3(0, 0, -1), frame * new Vector3(a, 0, -b), color, duration);
  16.         Debug.DrawLine(frame * new Vector3(a, 0, -b), frame * new Vector3(b, 0, -a), color, duration);
  17.         Debug.DrawLine(frame * new Vector3(b, 0, -a), frame * new Vector3(1, 0, 0), color, duration);
  18.         Debug.DrawLine(frame * new Vector3(1, 0, 0), frame * new Vector3(1, 0, scaledLength), color, duration);
  19.         Debug.DrawLine(frame * new Vector3(1, 0, scaledLength), frame * new Vector3(b, 0, scaledLength + a), color, duration);
  20.         Debug.DrawLine(frame * new Vector3(b, 0, scaledLength + a), frame * new Vector3(a, 0, scaledLength + b), color, duration);
  21.         Debug.DrawLine(frame * new Vector3(a, 0, scaledLength + b), frame * new Vector3(0, 0, scaledLength + 1), color, duration);
  22.         // Left side.
  23.         Debug.DrawLine(frame * new Vector3(0, 0, -1), frame * new Vector3(-a, 0, -b), color, duration);
  24.         Debug.DrawLine(frame * new Vector3(-a, 0, -b), frame * new Vector3(-b, 0, -a), color, duration);
  25.         Debug.DrawLine(frame * new Vector3(-b, 0, -a), frame * new Vector3(-1, 0, 0), color, duration);
  26.         Debug.DrawLine(frame * new Vector3(-1, 0, 0), frame * new Vector3(-1, 0, scaledLength), color, duration);
  27.         Debug.DrawLine(frame * new Vector3(-1, 0, scaledLength), frame * new Vector3(-b, 0, scaledLength + a), color, duration);
  28.         Debug.DrawLine(frame * new Vector3(-b, 0, scaledLength + a), frame * new Vector3(-a, 0, scaledLength + b), color, duration);
  29.         Debug.DrawLine(frame * new Vector3(-a, 0, scaledLength + b), frame * new Vector3(0, 0, scaledLength + 1), color, duration);
  30.  
  31.         // Top side.
  32.         Debug.DrawLine(frame * new Vector3(0, 0, -1), frame * new Vector3(0, a, -b), color, duration);
  33.         Debug.DrawLine(frame * new Vector3(0, a, -b), frame * new Vector3(0, b, -a), color, duration);
  34.         Debug.DrawLine(frame * new Vector3(0, b, -a), frame * new Vector3(0, 1, 0), color, duration);
  35.         Debug.DrawLine(frame * new Vector3(0, 1, 0), frame * new Vector3(0, 1, scaledLength), color, duration);
  36.         Debug.DrawLine(frame * new Vector3(0, 1, scaledLength), frame * new Vector3(0, b, scaledLength + a), color, duration);
  37.         Debug.DrawLine(frame * new Vector3(0, b, scaledLength + a), frame * new Vector3(0, a, scaledLength + b), color, duration);
  38.         Debug.DrawLine(frame * new Vector3(0, a, scaledLength + b), frame * new Vector3(0, 0, scaledLength + 1), color, duration);
  39.         // Bottom side.
  40.         Debug.DrawLine(frame * new Vector3(0, 0, -1), frame * new Vector3(0, -a, -b), color, duration);
  41.         Debug.DrawLine(frame * new Vector3(0, -a, -b), frame * new Vector3(0, -b, -a), color, duration);
  42.         Debug.DrawLine(frame * new Vector3(0, -b, - a), frame * new Vector3(0, -1, 0), color, duration);
  43.         Debug.DrawLine(frame * new Vector3(0, -1, 0), frame * new Vector3(0, -1, scaledLength), color, duration);
  44.         Debug.DrawLine(frame * new Vector3(0, -1, scaledLength), frame * new Vector3(0, -b, scaledLength + a), color, duration);
  45.         Debug.DrawLine(frame * new Vector3(0, -b, scaledLength + a), frame * new Vector3(0, -a, scaledLength + b), color, duration);
  46.         Debug.DrawLine(frame * new Vector3(0, -a, scaledLength + b), frame * new Vector3(0, 0, scaledLength + 1), color, duration);
  47.  
  48.         // Close ring.
  49.         Debug.DrawLine(frame * new Vector3(1, 0, 0), frame * new Vector3(b, a, 0), color, duration);
  50.         Debug.DrawLine(frame * new Vector3(b, a, 0), frame * new Vector3(a, b, 0), color, duration);
  51.         Debug.DrawLine(frame * new Vector3(a, b, 0), frame * new Vector3(0, 1, 0), color, duration);
  52.         Debug.DrawLine(frame * new Vector3(0, 1, 0), frame * new Vector3(-a, b, 0), color, duration);
  53.         Debug.DrawLine(frame * new Vector3(-a, b, 0), frame * new Vector3(-b, a, 0), color, duration);
  54.         Debug.DrawLine(frame * new Vector3(-b, a, 0), frame * new Vector3(-1, 0, 0), color, duration);
  55.         Debug.DrawLine(frame * new Vector3(-1, 0, 0), frame * new Vector3(-b, -a, 0), color, duration);
  56.         Debug.DrawLine(frame * new Vector3(-b, -a, 0), frame * new Vector3(-a, -b, 0), color, duration);
  57.         Debug.DrawLine(frame * new Vector3(-a, -b, 0), frame * new Vector3(0, -1, 0), color, duration);
  58.         Debug.DrawLine(frame * new Vector3(-0, -1, 0), frame * new Vector3(a, -b, 0), color, duration);
  59.         Debug.DrawLine(frame * new Vector3(a, -b, 0), frame * new Vector3(b, -a, 0), color, duration);
  60.         Debug.DrawLine(frame * new Vector3(b, -a, 0), frame * new Vector3(1, 0, 0), color, duration);
  61.  
  62.         // Far ring.
  63.         Debug.DrawLine(frame * new Vector3(1, 0, scaledLength), frame * new Vector3(b, a, scaledLength), color, duration);
  64.         Debug.DrawLine(frame * new Vector3(b, a, scaledLength), frame * new Vector3(a, b, scaledLength), color, duration);
  65.         Debug.DrawLine(frame * new Vector3(a, b, scaledLength), frame * new Vector3(0, 1, scaledLength), color, duration);
  66.         Debug.DrawLine(frame * new Vector3(0, 1, scaledLength), frame * new Vector3(-a, b, scaledLength), color, duration);
  67.         Debug.DrawLine(frame * new Vector3(-a, b, scaledLength), frame * new Vector3(-b, a, scaledLength), color, duration);
  68.         Debug.DrawLine(frame * new Vector3(-b, a, scaledLength), frame * new Vector3(-1, 0, scaledLength), color, duration);
  69.         Debug.DrawLine(frame * new Vector3(-1, 0, scaledLength), frame * new Vector3(-b, -a, scaledLength), color, duration);
  70.         Debug.DrawLine(frame * new Vector3(-b, -a, scaledLength), frame * new Vector3(-a, -b, scaledLength), color, duration);
  71.         Debug.DrawLine(frame * new Vector3(-a, -b, scaledLength), frame * new Vector3(0, -1, scaledLength), color, duration);
  72.         Debug.DrawLine(frame * new Vector3(-0, -1, scaledLength), frame * new Vector3(a, -b, scaledLength), color, duration);
  73.         Debug.DrawLine(frame * new Vector3(a, -b, scaledLength), frame * new Vector3(b, -a, scaledLength), color, duration);
  74.         Debug.DrawLine(frame * new Vector3(b, -a, scaledLength), frame * new Vector3(1, 0, scaledLength), color, duration);
  75.     }
RAW Paste Data