private Vector3 ChooseBestSnappingPosition() { var bestSnap = _potentialGridCellsWorld.Min(x => x.Distance); // todo: check up on this _potentialBestSnapCell = _potentialGridCellsWorld.FirstOrDefault(x => x.Distance == bestSnap); _currentGrid = _potentialBestSnapCell.CellGrid; // get the world position of that snappable stud: Vector3 position = _potentialBestSnapCell.WorldPos; // make sure entire brick is offset according to tube/stub being snapped to: position.x = position.x - _potentialBestSnapCell.TubeLocalPos.x; position.y = position.y - ModBrickMetrics.ThirdHeight / 2; position.z = position.z - _potentialBestSnapCell.TubeLocalPos.z; return(position); }
private bool SnapUpdate(out Vector3 position) { // snap cells positions are according to brick local position // convert them to grid local space _potentialGridCellsWorld = new List <ModBrickCell>(); _potentialGridCells = new List <Vector3I>(); foreach (var localSnapPosition in _bottomSnapCells) { var localSnapWorldPos = transform.TransformPoint(localSnapPosition); RaycastHit hit; if (Physics.Raycast(localSnapWorldPos, Vector3.down, out hit)) { var grid = hit.collider.gameObject.GetComponent <ModBrickGrid>(); if (grid != null) { var hitPos = hit.point; var hitPosGridLocal = grid.transform.InverseTransformPoint(hitPos); var gridCellPos = grid.ClosestGridCell(hitPosGridLocal); var taken = grid.IsTaken(gridCellPos); if (!taken) { _potentialGridCells.Add(gridCellPos); var gridCellWorldPos = grid.GridCellToWorldPos(gridCellPos); var cell = new ModBrickCell((gridCellWorldPos - transform.position).magnitude, grid, gridCellWorldPos, gridCellPos); cell.TubeWorldPos = localSnapWorldPos; cell.TubeLocalPos = localSnapPosition; _potentialGridCellsWorld.Add(cell); } } } } if (_potentialGridCellsWorld != null && _potentialGridCellsWorld.Count != 0) { position = ChooseBestSnappingPositionNew(); return(true); } else { // find closest fit } position = Vector3.zero; return(false); }