- Home /
UI world space text resolution
I'm trying to make a thing where you can see an object's name (and distance to the camera) kind of floating above it. So I made a world space canvas, scaled it down, put some text on it, and got, well this:
Which looks sort-of okay, but if I move it a bit further from the camera, the text becomes completely unreadable, like this:
And I want the text to be readable from some distance, too. So...can I fix this, or do I have to go back to the old GUI system and do the good (bad?) old translate-to-screen-space, draw Label in OnGUI method? In my example the object is 30 and 50 units away from the camera, which is really not that far. (it's a 10x10x10 cube if you're wondering about the size)
I've tried messing with the Dynamic Pixels Per Unit and Reference Pixels Per Unit variables on the Canvas Scaler, but that didn't seem to make a difference.
Oh, and for the curious, the World Object UI script that's on there looks like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WorldObjectUI : MonoBehaviour {
public Camera worldCamera;
public Image healthBar;
//public Image shieldBar;
public Text text;
public Health health;
// Use this for initialization
void Start () {
worldCamera = Camera.main;
}
// Update is called once per frame
void Update () {
if(health) healthBar.fillAmount = health.currentHitPoints/health.maxHitPoints;
text.text = transform.root.name + "\n" + Vector3.Distance(transform.position, worldCamera.transform.position).ToString("0.00m");
transform.LookAt(worldCamera.transform.position);
}
}
(Also, for somer reason that UI looks away from my camera and I have to rotate everything on it 180 degrees on the y-axis for it to be correct. Not really important, but if someone knows what's going on there I'd appreciate it, too).
Related, does anyone know if the new UI system has a way to place a UI object in world space, but always have it drawn the same size on screen?
This is a very important question for a very common scenario. Is anyone aware of any updates that are different from this?
Thank you Siaran for this btw!
Answer by siaran · Mar 26, 2015 at 01:10 AM
Well, here is one way to do it... http://blog.manapebbles.com/world-space-overlay-camera-in-unity/
Was hoping it would be easier with the new system than with the old one, but apparently not. If anyone knows of a better way I'd be glad to hear it but I guess I'll mark this as answered.