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);
}
}
}
}