// Update is called once per frame private void Update() { if (!BuilderActionComponentUI.ActiveSlot) { return; } if (MouseBehaviour.Mode == MouseMode.Build) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray); foreach (RaycastHit hit in hits) { if (hit.transform.tag == Tags.Ground) { float heightOffset = 0.03f; float factor = 0.5f; Vector3 position = hit.point; position.y = hit.point.y + heightOffset; position.x = hit.point.x; position.z = hit.point.z; Vector3 snapPoint = new Vector3( (float)Math.Round(position.x / factor, 1, MidpointRounding.AwayFromZero) * factor, hit.point.y + heightOffset, (float)Math.Round(position.z / factor, 1, MidpointRounding.AwayFromZero) * factor ); BuildingSlotUI buildingSlotUI = BuilderActionComponentUI .ActiveSlot .GetComponent <BuildingSlotUI>(); // Set preview game object position GameObject preview = buildingSlotUI.GetPreview(); preview.transform.position = snapPoint; BuildingPreviewBehaviour buildPreviewBehaviour = preview.GetComponent <BuildingPreviewBehaviour>(); // Check for collision if it is ok to place the building here. if (buildPreviewBehaviour.IsConstructable()) { // Left click if (Input.GetMouseButtonDown(0)) { buildingSlotUI.Place(snapPoint); buildingSlotUI.Unselect(); } } } } // Right click if (Input.GetMouseButtonDown(1)) { this.ClearSlotSelection(); } } }
/** * <summary> * Clear slot selection * </summary> * * <returns> * void * </returns> */ public void ClearSlotSelection() { BuildingSlotUI buildingSlotUI = BuilderActionComponentUI .ActiveSlot .GetComponent <BuildingSlotUI>(); buildingSlotUI.Unselect(); buildingSlotUI.HideRequirementsUI(); }