Advertisement
AnthonyGraceffa

QK HUD Manager

Mar 7th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. /* ----------------------------------------------------------------------------
  6.  * Main hud control, contains functions for updating HUD information on the player's screen
  7.  * these functions are designed to be called from whatever script needs to update them.
  8.  * ----------------------------------------------------------------------------
  9.  */
  10.  
  11. public class GameHUD : MonoBehaviour {
  12.  
  13.     public GameObject worldMapCanvas;               //All the game map elements
  14.     public GameObject gameMap;                      //The map iamge on a plane
  15.     public GameObject player;                       //reference to player
  16.  
  17.     GameObject mapCam;                              //Camera used for minimap
  18.     GameObject objectiveText;                       //Objective Text UI element
  19.     GameObject[] mapLabels;                         //Array of text taht appears on minimap
  20.  
  21.     GameObject compassCameraPoint;                  //Point at camera location used to calculate objective positions
  22.  
  23.     public GameObject testObjective;
  24.  
  25.     void Awake () {
  26.  
  27.         //Turn on UI stuff
  28.         worldMapCanvas.SetActive (true);
  29.  
  30.         //Fill mapLabels array
  31.         mapLabels = GameObject.FindGameObjectsWithTag ("worldMapLabel");
  32.  
  33.         //Set mapcam reference
  34.         mapCam = GameObject.Find ("mapCam");
  35.         //Set compassCameraPoint reference
  36.         compassCameraPoint = GameObject.Find ("compassCameraPoint");
  37.        
  38.     }
  39.    
  40.  
  41.     void Update () {
  42.         //this is for testing, call update map from player movements
  43.         rotateMapObjects ();
  44.  
  45.         //Set the compass indicator
  46.         setCompassValue (calculateObjectiveAngle (testObjective));
  47.         Debug.Log("Angle found: " + calculateObjectiveAngle(testObjective));
  48.  
  49.     }
  50.  
  51.     //Call this to update objective tet at top of the screen
  52.     public void UpdateObjectiveText (string newObjective) {
  53.         objectiveText.GetComponent<Text> ().text = "Objective: " + newObjective;
  54.     }
  55.  
  56.     //Rotates map labels so that the text is always right side up, call this from anything that rotates the camera
  57.     //right now its based on Player rotation, needs to be based on camera
  58.     public void rotateMapObjects () {
  59.         Quaternion newRotation;
  60.         foreach(GameObject curLabel in mapLabels) {
  61.             newRotation = Quaternion.Euler(new Vector3(90,0,-player.transform.rotation.eulerAngles.y));
  62.             curLabel.GetComponent<RectTransform>().rotation = newRotation;
  63.         }
  64.     }
  65.  
  66.     public void setCompassValue (float newValue) {
  67.         GameObject compass = GameObject.Find ("compassSlider");
  68.         if (newValue > 40) {
  69.             newValue = 40;
  70.         }
  71.         compass.GetComponent<Slider> ().value = newValue;
  72.     }
  73.  
  74.     public float calculateObjectiveAngle (GameObject objective) {
  75.         Vector3 pointToObjective;
  76.         Vector3 pointStraightFromCam;
  77.  
  78.         //Set points to determine which side of the player the vector towards the objective is going
  79.         GameObject camPointRight = GameObject.Find ("camPointRight");
  80.         GameObject camPointleft = GameObject.Find ("camPointLeft");
  81.  
  82.  
  83.         //create vector3 from player to objective and normalize it
  84.         pointToObjective = objective.gameObject.transform.position - compassCameraPoint.transform.position;
  85.         pointToObjective.Normalize ();
  86.  
  87.         //Set this vector to point straight away from camera
  88.         pointStraightFromCam = compassCameraPoint.transform.forward;
  89.         pointStraightFromCam.Normalize ();
  90.  
  91.         //if point to objective is closer to the left side return a negative angle, else return a positive one
  92.         if (Vector3.Distance (pointToObjective, camPointRight.transform.position) >= Vector3.Distance (pointToObjective, camPointleft.transform.position)) {
  93.             return -Vector3.Angle(pointStraightFromCam, pointToObjective);
  94.         }
  95.         else  {
  96.             return Vector3.Angle(pointStraightFromCam, pointToObjective);
  97.         }
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement