Advertisement
Neitri

Untitled

Oct 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. // author: Neitri
  2. // license: Public Domain
  3.  
  4. ItemComponent GetItemClosestToScreenPoint(Vector3 screenPoint)
  5. {
  6.     RaycastHit hit;
  7.     int circleRadius = 0;
  8.     int countOfRaysOnCircle = 1;
  9.     while (circleRadius < 50)
  10.     {
  11.         for (int i = 0; i < countOfRaysOnCircle; i++)
  12.         {
  13.             var offset = new Vector3(Mathf.Sin(Mathf.PI * 2 / countOfRaysOnCircle * i), Mathf.Cos(Mathf.PI * 2 / countOfRaysOnCircle * i), 0) * circleRadius;
  14.             var ray = Camera.Main.ScreenPointToRay(screenPoint + offset);
  15.             if (Physics.Raycast(ray, out hit, 10000))
  16.             {
  17.                 var item = hit.transform.root.GetComponentInChildren<ItemComponent>();
  18.                 if (item)
  19.                     return item;
  20.             }
  21.         }
  22.         circleRadius += 3;
  23.         countOfRaysOnCircle += 2;
  24.     }
  25.     return null;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement