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

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!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HUDTest : MonoBehaviour
{
    PlanetarySystem planetarySystemObject;
    Planet[] planets;

    public Texture hudReticle;
    Rect reticlePosition;

    public Texture navIndicator;
    bool[] navIndicatorVisible;

    Vector3[] screenPosition;

    // Use this for initialization
    void Start ()
    {
        planetarySystemObject = GameObject.FindObjectOfType<PlanetarySystem>() as PlanetarySystem;
        planets = planetarySystemObject.planets;

        reticlePosition = new Rect((Screen.width / 2) - (hudReticle.width / 2), (Screen.height / 2) - (hudReticle.height / 2), hudReticle.width, hudReticle.height);
        screenPosition = new Vector3[planetarySystemObject.NumberOfPlanets];
        navIndicatorVisible = new bool[planetarySystemObject.NumberOfPlanets];
    }
 
    // Update is called once per frame
    void Update ()
    {
        for (int i = 0; i < planetarySystemObject.NumberOfPlanets; i++)
        {
            screenPosition[i] = Camera.main.WorldToScreenPoint(planets[i].planetObject.transform.position);

            if (screenPosition[i].z > 0 && screenPosition[i].x > 0 && screenPosition[i].x < Screen.width && screenPosition[i].y > 0 && screenPosition[i].y < Screen.height)
            {
                navIndicatorVisible[i] = true;
            }
            else
                navIndicatorVisible[i] = false;
        }

    }

    void OnGUI()
    {
        //Draw a reticle to aid the player in navigation
        GUI.color = Color.green;
        GUI.DrawTexture(reticlePosition, hudReticle);

        //Display a box around the Planet's to help the player in navigation

            GUI.color = Color.yellow;
            for (int i = 0; i < planetarySystemObject.NumberOfPlanets; i++)
            {
                if (navIndicatorVisible[i] == true)
                {
                    GUI.DrawTexture(new Rect(screenPosition[i].x - (navIndicator.width / 2), screenPosition[i].y - (navIndicator.height / 2), navIndicator.width, navIndicator.height), navIndicator);
                }
            }
    }
}

Anyone?

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.

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.

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.

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