Advertisement
Cookie042

mass raycast hit test

Apr 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RaycastTesting : MonoBehaviour
  6. {
  7.  
  8.     public int count = 100;
  9.     public float spacing = .1f;
  10.     public LayerMask mask = 0;
  11.  
  12.     public int missingCasts = 0;
  13.  
  14.  
  15.     private void OnDrawGizmos()
  16.     {
  17.         missingCasts = 0;
  18.         Gizmos.matrix = transform.localToWorldMatrix;
  19.        
  20.         var sx = -spacing * count/2;
  21.         var sy = sx;
  22.  
  23.  
  24.         for (int i = 0; i < count; i++)
  25.         {
  26.             float y = sy + i * spacing;
  27.             for (int j = 0; j < count; j++)
  28.             {
  29.                 float x = sx + j * spacing;
  30.                 RaycastHit hit;
  31.  
  32.                 var p = new Vector3(x, y, 0);
  33.  
  34.  
  35.  
  36.                 if (Physics.Raycast(transform.TransformPoint(p) , transform.forward, out hit, 1000, mask))
  37.                 {
  38.                     Gizmos.color = new Color(0f, 0f, 1f, 0.1f);
  39.                     var hitPointLocal = transform.InverseTransformPoint(hit.point);
  40.                     Gizmos.DrawLine(p, hitPointLocal);
  41.  
  42.                 }
  43.                 else
  44.                 {
  45.                     Gizmos.color = Color.red;
  46.                     Gizmos.DrawRay(new Vector3(x, y, 0), Vector3.forward*100);
  47.                     missingCasts++;
  48.                 }
  49.  
  50.  
  51.             }
  52.         }
  53.  
  54.  
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement