private void Update() { if (showBox) { Ray ray = Camera.main.ScreenPointToRay(mousePos); RaycastHit groundHit; bool hitDetect = Physics.BoxCast( ray.origin, currentObject.transform.localScale, ray.direction, out groundHit, currentObject.transform.rotation, 10000, groundMask ); if (hitDetect) { boxLegal = !Physics.BoxCast( ray.origin, currentObject.transform.localScale / 2, ray.direction, currentObject.transform.rotation, 10000, illegalMask ); // Find the position of one of the corners. Vector3 corner = new Vector3( groundHit.point.x - currentObject.transform.localScale.x / 2, currentObject.transform.position.y, groundHit.point.z - currentObject.transform.localScale.z / 2 ); // Make sure the corner is aligned with the 8x8 grid. corner = GridUtilities.RoundToNearestSquare(corner); // Then convert back to the center position. Vector3 center = new Vector3( corner.x + currentObject.transform.localScale.x / 2, corner.y, corner.z + currentObject.transform.localScale.z / 2 ); currentObject.transform.position = center; currentObject.GetComponent <MeshRenderer>().material = boxLegal ? legalPlan : illegalPlan; } } }
private void LateUpdate() { // If the central grid squares are not, at minimum, minPixels in diameter, turn off the grid and be done. if (PixelSize.GetPixelSize(8, transform) < minPixels) { render.enabled = false; return; } // Otherwise, turn it on. render.enabled = true; // Find the center point where the camera is looking RaycastHit hit; Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit); Vector3 center = hit.point; // Round to the nearest 8 meter grid square and fix the Y height to one. center = GridUtilities.RoundToNearestSquare(center, 1); // Place the center of the grid at the center point. transform.position = center; }