public Clicked Click(Vector2 pos) { front_hit = GetFrontHit(pos); Clicked obj; if (!front_hit.nothing_clicked) { obj.clicked_object = front_hit.ray_hit.collider.gameObject; obj.isClicked = true; return obj; } else { obj.clicked_object = null; obj.isClicked = false; return obj; } }
private List <RaycastHitResult> RaycastAllInternal(Ray ray) { if (Map == null) { throw new InvalidOperationException("Cannot raycast against an unloaded map!"); } List <RaycastHitResult> returnResults = new List <RaycastHitResult>(); foreach (Room room in Map.Rooms) { foreach (var entity in room.Entities) { if (!Map.LayerIsVisible(entity.Layer)) { continue; } SceneComponent sceneEntity = entity as SceneComponent; if (sceneEntity == null) { continue; } Vector3 aabbMin, aabbMax; sceneEntity.GetAABB(out aabbMin, out aabbMax); float intersectionDist; if (RayIntersectsAABB(ray, aabbMin, aabbMax, out intersectionDist)) { RaycastHitResult result = new RaycastHitResult(); result.Distance = intersectionDist; result.Entity = entity; returnResults.Add(result); } } } // Sort the results - nearest to furthest. returnResults = returnResults.OrderBy(x => x.Distance).ToList(); return(returnResults); }
private RaycastHitResult GetFrontHit(Vector2 pos) { RaycastHitResult result = new RaycastHitResult(); Vector3 screenPos = new Vector3(pos.x, pos.y, 0f); click_pos = new Vector3[cameras.Length]; RaycastHit2D[][] hits = new RaycastHit2D[cameras.Length][]; int hit_number = 0; for (int i = 0; i < cameras.Length; i++) { click_pos[i] = cameras[i].ScreenToWorldPoint(screenPos); hits[i] = Physics2D.LinecastAll(click_pos[i], click_pos[i]); hit_number += hits[i].Length; } if (hit_number <= 0) { if (showDebug) Debug.Log("Nothing clicked"); result.nothing_clicked = true; return result; } bool hit = false; int[][] curSortingLayers = new int[hits.Length][]; int[][] curSortingOrders = new int[hits.Length][]; int index_x = 0, index_y = 0; int topSortingLayer = 0; int topSortingOrder = 0; for (int i = 0; i < hits.Length; i++) { curSortingLayers[i] = new int[hits[i].Length]; curSortingOrders[i] = new int[hits[i].Length]; for (int j = 0; j < hits[i].Length; j++) { spriteRenderer = hits[i][j].collider.gameObject.GetComponent<SpriteRenderer>(); if (spriteRenderer != null) { hit = true; curSortingLayers[i][j] = spriteRenderer.sortingLayerID; curSortingOrders[i][j] = spriteRenderer.sortingOrder; if (curSortingLayers[i][j] >= topSortingLayer) topSortingLayer = curSortingLayers[i][j]; } } } if (!hit) { if (showDebug) Debug.Log("Nothing clicked"); result.nothing_clicked = true; return result; } for (int i = 0; i < hits.Length; i++) { for (int j = 0; j < hits[i].Length; j++) { if (curSortingOrders[i][j] >= topSortingOrder && curSortingLayers[i][j] >= topSortingLayer) { topSortingOrder = curSortingOrders[i][j]; index_x = i; index_y = j; } } } result.ray_hit = hits[index_x][index_y]; result.nothing_clicked = false; return result; }
private List<RaycastHitResult> RaycastAllInternal(Ray ray) { if (Map == null) throw new InvalidOperationException("Cannot raycast against an unloaded map!"); List<RaycastHitResult> returnResults = new List<RaycastHitResult>(); foreach (Room room in Map.Rooms) { foreach (var entity in room.Entities) { if (!Map.LayerIsVisible(entity.Layer)) continue; SceneComponent sceneEntity = entity as SceneComponent; if (sceneEntity == null) continue; Vector3 aabbMin, aabbMax; sceneEntity.GetAABB(out aabbMin, out aabbMax); float intersectionDist; if (RayIntersectsAABB(ray, aabbMin, aabbMax, out intersectionDist)) { RaycastHitResult result = new RaycastHitResult(); result.Distance = intersectionDist; result.Entity = entity; returnResults.Add(result); } } } // Sort the results - nearest to furthest. returnResults = returnResults.OrderBy(x => x.Distance).ToList(); return returnResults; }