private void Update() { switch (mouseMode) { case (MouseMode.Placement): //Shoot a ray to the mouse RaycastHit hitInfo; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 localUp = cursor.transform.up; Vector3 localForward = cursor.transform.forward; if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, placementLayerMask)) { cursor.transform.LookAt(cursor.transform.position + localForward, localUp); //Snap to the grid cursor.transform.position = grid.getGridPoint(hitInfo.point); //New offset so it fits on the grid spot Vector3 pos = new Vector3(0f, 4.5f, 0f); cursor.transform.position += pos; localUp = hitInfo.normal; cursor.transform.position += hitInfo.normal * 0.5f; if (bldPlacer.checkPosition(cursor)) { //Set the material of the cursor to the placeable color mesh.material = placeable; foreach (Transform child in cursor.transform) { child.gameObject.layer = 8; childMesh = child.GetComponent <MeshRenderer>(); childMesh.material = placeable; } } else { mesh.material = unplaceable; foreach (Transform child in cursor.transform) { child.gameObject.layer = 8; childMesh = child.GetComponent <MeshRenderer>(); childMesh.material = unplaceable; } } } //Rotate Left if (Input.GetKeyDown(KeyCode.A)) { Vector3 temp = cursor.transform.forward; temp.x = -cursor.transform.forward.z; temp.z = cursor.transform.forward.x; localForward = temp; cursor.transform.forward = localForward; } //Rotate Right if (Input.GetKeyDown(KeyCode.D)) { Vector3 temp = cursor.transform.forward; temp.x = cursor.transform.forward.z; temp.z = -cursor.transform.forward.x; localForward = temp; cursor.transform.forward = localForward; } if (Input.GetKeyDown(KeyCode.Mouse0)) { //Replace the raycast with the ability to detect if the mouse is over a ui element if (bldPlacer.checkPosition(cursor) && Physics.Raycast(ray, out hitInfo, Mathf.Infinity, placementLayerMask)) { bldPlacer.PlaceBuildingNear(cursor); } } break; } }