Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: C#  |  size: 0.61 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. private T FindClosest<T>(float maxDistance = Mathf.Infinity) where T : Component  {
  2.                 state = states.searching;
  3.                 float i = 0f;
  4.                 RaycastHit info;
  5.                 T closest = null;
  6.                 float shortestdistance = maxDistance;
  7.                 while (i < 2.0f) {
  8.                         if (Physics.Raycast(transform.position, new Vector3(Mathf.Cos(i * Mathf.PI), 0f, Mathf.Sin(i * Mathf.PI)),
  9.                                 out info, maxDistance) &&
  10.                                 info.collider.GetComponent<T>() != null &&
  11.                                 info.distance < shortestdistance) {
  12.                                         closest = info.collider.GetComponent<T>();
  13.                                         shortestdistance = info.distance;
  14.                                         Debug.Log("ASG");
  15.                         }
  16.                         i+=0.2f;
  17.                 }
  18.                 return closest;
  19.         }