/// <summary> /// Call this method each update to make sure the bee doesn't fly off the screen borders. /// </summary> void KeepBeeWithinViewport() { // Get the bee's current and future positions (we are projecting two update frames ahead). Vector3 currentPosition = Camera.main.WorldToViewportPoint(transform.position); Vector3 nextPosition = Camera.main.WorldToViewportPoint(transform.position + (velocity * Time.deltaTime * 2)); // Check if the bee is heading off screen in the next few frames. Rect viewport = new Rect(0f, 0f, 1f, 1f); if (viewport.Contains(currentPosition) && !viewport.Contains(nextPosition)) { // If the bee has reached the edge of the screen, we need to flip its velocity. if (nextPosition.x <= 0f || nextPosition.x >= 1f) { velocity.x *= -1f; } if (nextPosition.y <= 0f || nextPosition.y >= 1f) { velocity.y *= -1f; } // Stop following the flight path. flightPath.Clear(); } }
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; } } } }