Transform beeTransform; // Transform component of the bee we are tracking. void Update() { // Check if the bee is still valid. if (beeTransform == null || beeController.IsStunned()) { // Stop tracking if the bee is gone or is stunned. Destroy(gameObject); return; } // Check if the bee is visible yet. Vector3 viewportPosition = Camera.main.WorldToViewportPoint(beeTransform.position); if (viewportPosition.x >= 0f && viewportPosition.x <= 1f && viewportPosition.y >= 0f && viewportPosition.y <= 1f) { // Bee is in view, we can destroy the icon now. Destroy(gameObject); return; } // Update icon screen position. Vector3 screenPosition = Camera.main.ViewportToScreenPoint(viewportPosition); screenPosition.x = Mathf.Clamp(screenPosition.x, screenOffset, Screen.width - screenOffset); screenPosition.y = Mathf.Clamp(screenPosition.y, screenOffset, Screen.height - screenOffset); transform.position = screenPosition; }
bool drag = false; // Are we currently dragging. void Update() { // Check if we are currently dragging the mouse. if (!drag) { // We only enter drag mode during gameplay and when the player hold the left mouse button. if (GameplayManager.Instance.CanPlay() && Input.GetMouseButtonDown(0)) { //Create 3D ray pointing into the world at the mouse position Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Check if our ray is hitting anything in the scene RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, 1000f)) { //Check if the ray hits a bee. If so, we have 'selected the bee if (hitInfo.transform.CompareTag("Bee")) { //Make sure the bee is not stunned BeeController bee = hitInfo.transform.GetComponent <BeeController>(); if (!bee.IsStunned()) { //Append waypoints to path for this bee, //starting at current position currentPath = hitInfo.transform.GetComponentInChildren <FlightPath>(); currentPath.Clear(); currentPath.Append(hitInfo.transform.position); //Enter dragmode, keep appending waypoints to bee's path drag = true; } } } } } else { // Check if we should stay in drag mode. We will exit if the bee has reached a honeycomb // or the end of its path. if (currentPath == null || currentPath.EndReached() || currentPath.Waypoints.Count == 0) { drag = false; } else { // Create a 3D ray pointing into the world and test against the play plane, which is // set at position with z equals 0. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; playPlane.Raycast(ray, out hitInfo, 1000f); // Check if we have dragged far enough from our last waypoint. Vector3 currentPosition = hitInfo.point; Vector3 lastPosition = currentPath.GetLastPosition(); if (Vector3.Distance(currentPosition, lastPosition) > segmentLength) { // Keep adding fixed-length segments up to the current position. This way the // dash-line for the path will be consistent instead of looking stretched. float dragLength = Vector3.Distance(currentPosition, lastPosition); Vector3 direction = Vector3.Normalize(currentPosition - lastPosition); float totalLength = segmentLength; while (totalLength < dragLength) { currentPath.Append(lastPosition + (direction * totalLength)); totalLength += segmentLength; } } // Check if the player has stopped dragging. if (Input.GetMouseButtonUp(0)) { // Try to snap the bee's path to the closest honeycomb. Beehive.Instance.SnapToHoneycomb(currentPath); // Exit drag mode. drag = false; } } } }