Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // author: Neitri
- // license: Public Domain
- ItemComponent GetItemClosestToScreenPoint(Vector3 screenPoint)
- {
- RaycastHit hit;
- int circleRadius = 0;
- int countOfRaysOnCircle = 1;
- while (circleRadius < 50)
- {
- for (int i = 0; i < countOfRaysOnCircle; i++)
- {
- var offset = new Vector3(Mathf.Sin(Mathf.PI * 2 / countOfRaysOnCircle * i), Mathf.Cos(Mathf.PI * 2 / countOfRaysOnCircle * i), 0) * circleRadius;
- var ray = Camera.Main.ScreenPointToRay(screenPoint + offset);
- if (Physics.Raycast(ray, out hit, 10000))
- {
- var item = hit.transform.root.GetComponentInChildren<ItemComponent>();
- if (item)
- return item;
- }
- }
- circleRadius += 3;
- countOfRaysOnCircle += 2;
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement