override public void updateRender() { // Get heading float heading = body.rotation.eulerAngles.y; // Get track Vector3 velocityInPlane = new Vector3(rb.velocity.x, 0, rb.velocity.z); velocityInPlane = Quaternion.AngleAxis(-heading, Vector3.up) * velocityInPlane; float track = -Mathf.Atan2(velocityInPlane.x, velocityInPlane.z) * Mathf.Rad2Deg; // Apply track ball rotation trackBall.gameObject.SetActive(Mathf.Abs(velocityInPlane.z) > 0.1); // keep heading in range [1,360] while (heading >= 360.5) { heading -= 360; } while (heading < 0.5) { heading += 360; } // keep track in range [1,360] while (track >= 360) { track -= 360; } while (track < 0) { track += 360; } // calculate and apply rotation of compass rose float rotation = rotationAtZero + heading; rose.localEulerAngles = new Vector3(rose.transform.localEulerAngles.x, rose.transform.localEulerAngles.y, rotation); // apply track ball rotation trackBall.localEulerAngles = new Vector3(trackBall.localEulerAngles.x, trackBall.localEulerAngles.y, track); // update heading readout readout.text = (heading + 0.5f) < 10 ? "00" + ((int)(heading + 0.5f)).ToString() : (heading + 0.5f) < 100 ? "0" + ((int)(heading + 0.5f)).ToString() : ((int)(heading + 0.5f)).ToString(); // retrive map range float range = rangeController.getRange(); // auto-fill the range rings labels for (int i = 0; i < ranges.Length; ++i) { ranges[i].text = (range / ((float)(ranges.Length)) * (float)(i + 1)).ToString(); } }
override public void updateRender() { // Retrieve displayed range range = rangeController.getRange(); // in Nautical Miles float rangeInMeters = range * 1852.0f; // Rotate map by H/C heading float mapRotation = helicopter.rotation.eulerAngles.y; map.localEulerAngles = new Vector3(0, 0, mapRotation); // Update waypoints and legs on map int imageIdx = 0; for (imageIdx = 0; imageIdx < waypointTransforms.Count; imageIdx++) { if (imageIdx < waypointImages.Length) { // WAYPOINT // Transform waypoint relative to H/C Vector3 relativeWptPos = waypointTransforms[imageIdx].position - helicopter.position; // Position waypoint without consideration for heading. The whole map will be rotated later for heading. float relativeX = relativeWptPos.x / rangeInMeters * rangeInPixels; float relativeY = relativeWptPos.z / rangeInMeters * rangeInPixels; waypointImages[imageIdx].localPosition = new Vector3(relativeX, relativeY, 0); // apply -heading rotation to keep waypoint images upward on the map after heading rotation is applied waypointImages[imageIdx].localEulerAngles = new Vector3(0, 0, -mapRotation); waypointImages[imageIdx].gameObject.SetActive(true); // Set color of waypoint if (imageIdx == flightPlanController.getActiveWaypoint()) { waypointImages[imageIdx].GetComponent <Image>().color = Color.magenta; } else { waypointImages[imageIdx].GetComponent <Image>().color = Color.white; } // LEGS // https://answers.unity.com/questions/865927/draw-a-2d-line-in-the-new-ui.html if (imageIdx > 0) { // Find coordinates of previous waypoint Vector3 prev_relativeWptPos = waypointTransforms[imageIdx - 1].position - helicopter.position; float prev_relativeX = prev_relativeWptPos.x / rangeInMeters * rangeInPixels; float prev_relativeY = prev_relativeWptPos.z / rangeInMeters * rangeInPixels; Vector3 pointA = new Vector3(prev_relativeX, prev_relativeY, 0); Vector3 pointB = new Vector3(relativeX, relativeY, 0); // leg vector Vector3 differenceVector = pointB - pointA; // draw leg (resize rectangular image on map) legImages[imageIdx - 1].sizeDelta = new Vector2(differenceVector.magnitude, lineWidth); legImages[imageIdx - 1].pivot = new Vector2(0, 0.5f); legImages[imageIdx - 1].localPosition = pointA; float angle = Mathf.Atan2(differenceVector.y, differenceVector.x) * Mathf.Rad2Deg + mapRotation; legImages[imageIdx - 1].rotation = Quaternion.Euler(0, 0, angle); legImages[imageIdx - 1].gameObject.SetActive(true); // set leg color if (imageIdx == flightPlanController.getActiveWaypoint()) { legImages[imageIdx - 1].GetComponent <Image>().color = Color.magenta; } else { legImages[imageIdx - 1].GetComponent <Image>().color = Color.white; } } } } // hide remaining waypoints images for (int i = imageIdx; i < waypointImages.Length; ++i) { waypointImages[i].gameObject.SetActive(false); } // hide remaining legs images for (int i = imageIdx - 1; i < legImages.Length; ++i) { if (i >= 0) { legImages[i].gameObject.SetActive(false); } } }