void MouseClicked() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); if (pause != true && toolDelay == 0) { //0 - drill if (toolSelected == 0) { if (Physics.Raycast(ray, out hit, 1000.0f)) { if (hit.transform.gameObject.tag == "tooth") //checks if tooth is toolDelay { pause = true; int result = tooth.RemoveVoxel(hit, 0); score += result; if (soundDelay < 0) { this.GetComponent <AudioSource>().PlayOneShot(soundEffects[0]); soundDelay = 6; } if (result == -1000) { t = 0; } pause = false; toolDelay += delayAmount; } } } //1 - polish else if (toolSelected == 1) { if (Physics.Raycast(ray, out hit, 1000.0f)) { if (hit.transform.gameObject.tag == "tooth") //checks if tooth is toolDelay { int result = tooth.RemoveVoxel(hit, 1); //removes voxel score += result; toolDelay += delayAmount; if (result != 0) { if (soundDelay < 0) { this.GetComponent <AudioSource>().PlayOneShot(soundEffects[1]); soundDelay = 6; } } } } } //2 - filler else if (toolSelected == 2) { if (Physics.Raycast(ray, out hit, 1000.0f)) { if (hit.transform.gameObject.tag == "tooth") //checks if tooth is toolDelay { score += tooth.AddVoxel(hit); //adds voxel toolDelay += delayAmount; if (delayAmount == 0) { toolDelay += 5; } if (soundDelay < 0) { this.GetComponent <AudioSource>().PlayOneShot(soundEffects[2]); soundDelay = 6; } } } } } }
private void UpdateLaser() { Camera camera = CameraExtensions.FindCameraUnderMouse(); if (camera == null) { return; } Vector3 mousePosition = Input.mousePosition; mousePosition.z = 50; Vector3 target = camera.ScreenToWorldPoint(mousePosition); Vector3 delta = target - laser.transform.position; if (delta.magnitude > 30) { delta = delta.normalized * 30.0f; target = laser.transform.position + delta; } // First check if the mouse pointer is over a mesh Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; bool hit = false; VoxelRenderer voxelRenderer = null; if (Physics.Raycast(ray, out hitInfo, 50.0f)) { target = hitInfo.point; voxelRenderer = hitInfo.collider.gameObject.GetComponentInParent <VoxelRenderer>(); // Protect a second ray to see if the point we are aiming at would collide first ray = new Ray(laser.transform.position, target - laser.transform.position); if (Physics.Raycast(ray, out hitInfo, 50.0f)) { target = hitInfo.point; voxelRenderer = hitInfo.collider.gameObject.GetComponentInParent <VoxelRenderer>(); } hit = true; } if (!hit) { // If not protect a ray from the origin to the target ray = new Ray(laser.transform.position, delta); if (Physics.Raycast(ray, out hitInfo, delta.magnitude)) { target = hitInfo.point; voxelRenderer = hitInfo.collider.gameObject.GetComponentInParent <VoxelRenderer>(); hit = true; } } if (hit && voxelRenderer != null) { VoxelMap voxelMap = voxelRenderer.VoxelMap; Vector3 localPosition = target - voxelRenderer.gameObject.transform.position; Vector3 point; point.x = localPosition.x / voxelMap.Scale - voxelMap.Offset.x; point.y = localPosition.y / voxelMap.Scale - voxelMap.Offset.y; point.z = localPosition.z / voxelMap.Scale - voxelMap.Offset.z; // Clamp to the bounds of the map int x = Mathf.Clamp(Mathf.RoundToInt(point.x), 0, voxelMap.Columns); int y = Mathf.Clamp(Mathf.RoundToInt(point.y), 0, voxelMap.Rows); int z = Mathf.Clamp(Mathf.RoundToInt(point.z), 0, voxelMap.Pages); IntVector3 nearestVoxel; if (voxelMap.FindNearestVoxel(localPosition, out nearestVoxel)) { if (!lastVoxel.Equals(nearestVoxel)) { lastVoxel = nearestVoxel; lastVoxelTimer = 0; } else { lastVoxelTimer += Time.deltaTime; if (lastVoxelTimer > 0.1) { Vector3 position = nearestVoxel; position *= voxelMap.Scale; position += voxelMap.Offset; position += voxelRenderer.gameObject.transform.position; DebugUI.DrawCubeCentred(position, new Vector3(voxelMap.Scale, voxelMap.Scale, voxelMap.Scale), voxelRenderer.gameObject.transform.rotation, Color.red); voxelRenderer.RemoveVoxel(nearestVoxel); lastVoxel = IntVector3.UNDEFINED; } } } else { lastVoxel = IntVector3.UNDEFINED; } } else { lastVoxel = IntVector3.UNDEFINED; } delta = target - laser.transform.position; Vector3 scale = laser.transform.localScale; scale.z = delta.magnitude; laser.transform.localScale = scale; laser.transform.LookAt(target); delta = delta.normalized * (delta.magnitude - 0.1f); collision.transform.position = laser.transform.position + delta; ParticleSystem[] particles = collision.GetComponentsInChildren <ParticleSystem>(); foreach (var ps in particles) { var emission = ps.emission; emission.enabled = hit; } }