Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- /* ----------------------------------------------------------------------------
- * Main hud control, contains functions for updating HUD information on the player's screen
- * these functions are designed to be called from whatever script needs to update them.
- * ----------------------------------------------------------------------------
- */
- public class GameHUD : MonoBehaviour {
- public GameObject worldMapCanvas; //All the game map elements
- public GameObject gameMap; //The map iamge on a plane
- public GameObject player; //reference to player
- GameObject mapCam; //Camera used for minimap
- GameObject objectiveText; //Objective Text UI element
- GameObject[] mapLabels; //Array of text taht appears on minimap
- GameObject compassCameraPoint; //Point at camera location used to calculate objective positions
- public GameObject testObjective;
- void Awake () {
- //Turn on UI stuff
- worldMapCanvas.SetActive (true);
- //Fill mapLabels array
- mapLabels = GameObject.FindGameObjectsWithTag ("worldMapLabel");
- //Set mapcam reference
- mapCam = GameObject.Find ("mapCam");
- //Set compassCameraPoint reference
- compassCameraPoint = GameObject.Find ("compassCameraPoint");
- }
- void Update () {
- //this is for testing, call update map from player movements
- rotateMapObjects ();
- //Set the compass indicator
- setCompassValue (calculateObjectiveAngle (testObjective));
- Debug.Log("Angle found: " + calculateObjectiveAngle(testObjective));
- }
- //Call this to update objective tet at top of the screen
- public void UpdateObjectiveText (string newObjective) {
- objectiveText.GetComponent<Text> ().text = "Objective: " + newObjective;
- }
- //Rotates map labels so that the text is always right side up, call this from anything that rotates the camera
- //right now its based on Player rotation, needs to be based on camera
- public void rotateMapObjects () {
- Quaternion newRotation;
- foreach(GameObject curLabel in mapLabels) {
- newRotation = Quaternion.Euler(new Vector3(90,0,-player.transform.rotation.eulerAngles.y));
- curLabel.GetComponent<RectTransform>().rotation = newRotation;
- }
- }
- public void setCompassValue (float newValue) {
- GameObject compass = GameObject.Find ("compassSlider");
- if (newValue > 40) {
- newValue = 40;
- }
- compass.GetComponent<Slider> ().value = newValue;
- }
- public float calculateObjectiveAngle (GameObject objective) {
- Vector3 pointToObjective;
- Vector3 pointStraightFromCam;
- //Set points to determine which side of the player the vector towards the objective is going
- GameObject camPointRight = GameObject.Find ("camPointRight");
- GameObject camPointleft = GameObject.Find ("camPointLeft");
- //create vector3 from player to objective and normalize it
- pointToObjective = objective.gameObject.transform.position - compassCameraPoint.transform.position;
- pointToObjective.Normalize ();
- //Set this vector to point straight away from camera
- pointStraightFromCam = compassCameraPoint.transform.forward;
- pointStraightFromCam.Normalize ();
- //if point to objective is closer to the left side return a negative angle, else return a positive one
- if (Vector3.Distance (pointToObjective, camPointRight.transform.position) >= Vector3.Distance (pointToObjective, camPointleft.transform.position)) {
- return -Vector3.Angle(pointStraightFromCam, pointToObjective);
- }
- else {
- return Vector3.Angle(pointStraightFromCam, pointToObjective);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement