So I’m trying to create markers or waypoint using GUILabels and I keep getting this bug that I’m not sure how to fix. The problem is that whenever the player looks up or down the marker moves with it but id want it to be anchored to the a certain position but I’m not sure how to do that. The image below shows what I mean. For instance, I’d like the ‘237’ to be attached to the plane, but only in the y position. Anyway to anchor the GUILabel to that point?
My next issue is that the markers kind of reflect if I turn around, the two images below explain what I’m talking about.
As you can see if I turn around they are reflected and in the wrong position. Is there anyway I can fix these errors?
Here is my script for the GUI Markers
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BombLocations : MonoBehaviour {
public Transform BombSpot1;
public Transform BombSpot2;
public Transform BombSpot3;
public Transform playerSpot;
public Vector3 Bomb1Location;
public Vector3 Bomb2Location;
public Vector3 Bomb3Location;
public float Distance1;
public float Distance2;
public float Distance3;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Bomb1Location = Camera.main.WorldToScreenPoint(BombSpot1.position);
Bomb2Location = Camera.main.WorldToScreenPoint(BombSpot2.position);
Bomb3Location = Camera.main.WorldToScreenPoint(BombSpot3.position);
}
void OnGUI()
{
Distance1 = (int)Vector3.Distance(playerSpot.position, BombSpot1.position);
Distance2 = (int)Vector3.Distance(playerSpot.position, BombSpot2.position);
Distance3 = (int)Vector3.Distance(playerSpot.position, BombSpot3.position);
GUI.Label(new Rect(Bomb1Location.x, 101,100,20), Distance1.ToString("") + "m");
GUI.Label(new Rect(Bomb1Location.x + 3, 116, 100, 20), "▲");
GUI.Label(new Rect(Bomb2Location.x, Bomb2Location.y, 100, 20), Distance2.ToString("") + "m");
GUI.Label(new Rect(Bomb2Location.x + 3, Bomb2Location.y + 15, 100, 20), "▲");
GUI.Label(new Rect(Bomb3Location.x, Bomb3Location.y, 100, 20), Distance3.ToString("") + "m");
GUI.Label(new Rect(Bomb3Location.x + 3, Bomb3Location.y + 15, 100, 20), "▲");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SystemMarkers : MonoBehaviour
{
public GameObject[] Planet;
public Transform playerLoc;
public Vector3[] planetLoc;
void Start()
{
Planet = GameObject.FindGameObjectsWithTag("Planet");
planetLoc = new Vector3[Planet.Length];
}
void Update()
{
for (int i = 0; i < Planet.Length; i++)
{
planetLoc[i] = Camera.main.WorldToScreenPoint(Planet[i].transform.position);
}
}
void OnGUI()
{
for (int i = 0; i < Planet.Length; i++)
{
GUI.color = Color.cyan;
GUI.Label(new Rect(planetLoc[i].x + 6, Screen.height - planetLoc[i].y, 100, 20), "planet");
GUI.Label(new Rect(planetLoc[i].x - 6, Screen.height - planetLoc[i].y, 100, 20), "▲");
}
}
}
This is the code I used. I ran into some issues while making this: just using the y value in planetLoc resulted in the label moving as if the screen were upside down, so I had to subtract its amount from the height of the screen.
However, I wonder if anyone could help me make it a bit better? Currently it isn’t being as accurate as I’d like: You can see the label is just under the planet rather than right on top like I want. It’s more noticeable the farther away the target object is from the camera. If anyone has any advice on how I could fix that, I’m open to suggestions.
I also have no idea how to get it to stop showing the labels for planets that are behind the camera.
I managed to answer my own question about the reversed labels. Turns out the z value from WorldToScreenPoint is negative if the target is behind the camera. A simple if statement lets me only make the markers while the target is in front of the camera.
I also fixed the lag in the tags by changing update() to lateupdate().