private void FindNearGridOnDirection(LevelGrid grid, NearGridDirection dir) { int layerMask = 1 << 9 | 1 << 10; RaycastHit hit; Vector3 raycastPos = grid.position + 0.5f * grid.direction; Quaternion rotation = Quaternion.FromToRotation(Vector3.up, grid.direction); Vector3 direction = Vector3.back; switch (dir) { case NearGridDirection.BACK: { direction = Vector3.back; break; } case NearGridDirection.FORWARD: { direction = Vector3.forward; break; } case NearGridDirection.LEFT: { direction = Vector3.left; break; } case NearGridDirection.RIGHT: { direction = Vector3.right; break; } } if (Physics.Raycast(raycastPos, rotation * direction, out hit, 1f, layerMask)) { GridSplitter cube = hit.collider.GetComponent <GridSplitter>(); grid.SetNearGrid(cube.GetGridAtPosition(hit.point), dir); //grid.setDebugInfo(raycastPos, hit.point, dir); } else if (Physics.Raycast(raycastPos, grid.position + rotation * direction - raycastPos, out hit, 2f, layerMask)) { GridSplitter cube = hit.collider.GetComponent <GridSplitter>(); grid.SetNearGrid(cube.GetGridAtPosition(hit.point), dir); //grid.setDebugInfo(raycastPos, hit.point, dir); } else if (Physics.Raycast(raycastPos - grid.direction + rotation * direction, rotation * ((-1) * direction), out hit, 1f, layerMask)) { GridSplitter cube = hit.collider.GetComponent <GridSplitter>(); grid.SetNearGrid(cube.GetGridAtPosition(hit.point), dir); //grid.setDebugInfo(raycastPos - grid.direction + rotation * direction, hit.point, dir); } }
//获取对应方向的LevelGrid public LevelGrid GetNearGrid(NearGridDirection dir) { return(nearGrids[(int)dir - 1]); }
public void setDebugInfo(Vector3 pos, Vector3 hit, NearGridDirection dir) { raycastPos[(int)dir - 1] = pos; hitPos[(int)dir - 1] = hit; }
//设置对应方向的LevelGrid public void SetNearGrid(LevelGrid near, NearGridDirection dir) { nearGrids[(int)dir - 1] = near; }