Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED]Nav Indicator HUD Icon not staying on Nav Target

Discussion in 'Scripting' started by BradMick, Jun 1, 2016.

  1. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    upload_2016-6-1_12-12-39.png

    The picture explains it all. The nav indicator is rock solid on the x axis, but not on the y. I'm not sure why...Here's the code.

    Do you think I need to do some calculations to better pin down the y position? I figured the WorldToScreen would handle all of that...based on how I understood it.

    Anyway, code!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class HUDTest : MonoBehaviour
    6. {
    7.     PlanetarySystem planetarySystemObject;
    8.     Planet[] planets;
    9.  
    10.     public Texture hudReticle;
    11.     Rect reticlePosition;
    12.  
    13.     public Texture navIndicator;
    14.     bool[] navIndicatorVisible;
    15.  
    16.     Vector3[] screenPosition;
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         planetarySystemObject = GameObject.FindObjectOfType<PlanetarySystem>() as PlanetarySystem;
    22.         planets = planetarySystemObject.planets;
    23.  
    24.         reticlePosition = new Rect((Screen.width / 2) - (hudReticle.width / 2), (Screen.height / 2) - (hudReticle.height / 2), hudReticle.width, hudReticle.height);
    25.         screenPosition = new Vector3[planetarySystemObject.NumberOfPlanets];
    26.         navIndicatorVisible = new bool[planetarySystemObject.NumberOfPlanets];
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update ()
    31.     {
    32.         for (int i = 0; i < planetarySystemObject.NumberOfPlanets; i++)
    33.         {
    34.             screenPosition[i] = Camera.main.WorldToScreenPoint(planets[i].planetObject.transform.position);
    35.  
    36.             if (screenPosition[i].z > 0 && screenPosition[i].x > 0 && screenPosition[i].x < Screen.width && screenPosition[i].y > 0 && screenPosition[i].y < Screen.height)
    37.             {
    38.                 navIndicatorVisible[i] = true;
    39.             }
    40.             else
    41.                 navIndicatorVisible[i] = false;
    42.         }
    43.  
    44.     }
    45.  
    46.     void OnGUI()
    47.     {
    48.         //Draw a reticle to aid the player in navigation
    49.         GUI.color = Color.green;
    50.         GUI.DrawTexture(reticlePosition, hudReticle);
    51.  
    52.         //Display a box around the Planet's to help the player in navigation
    53.  
    54.             GUI.color = Color.yellow;
    55.             for (int i = 0; i < planetarySystemObject.NumberOfPlanets; i++)
    56.             {
    57.                 if (navIndicatorVisible[i] == true)
    58.                 {
    59.                     GUI.DrawTexture(new Rect(screenPosition[i].x - (navIndicator.width / 2), screenPosition[i].y - (navIndicator.height / 2), navIndicator.width, navIndicator.height), navIndicator);
    60.                 }
    61.             }
    62.     }
    63. }
    64.  
     
  2. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Anyone?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    your camera looks "twisted" since the horizon line is not flat, which would make a line of objects have different y positions in screen space... it's hard to see what you're trying to do in that screen shot since there is no indication of how/where the planets are arranged in the scene.
     
  4. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    the planets are all 0 on the y axis. the idea is that the nav indicator stays over the top of the planet...because the planets are so far away, it's critical that the y position be accurate. In the image, the spaceship is pitching
    'up' and yawing to the right. The nav indicator should remain over the top of the planet, which is at y = 0 and roughly...0.38 AU in the distance. The idea is that the nav indicator stays on top of the planet even when it's not visible for accurate navigation.
     
  5. BradMick

    BradMick

    Joined:
    Apr 2, 2014
    Posts:
    113
    Solved....it was literally this simple:

    Screen.height - screenPosition.y - (navIndicator.height / 2)

    I wasn't subtracting the screen position for the height...once I did that, BOOM! fixed.

    upload_2016-6-2_8-32-18.png


    There you can see the planet is in the center of the nav icon.