Hello,
I have this code but I don’t know what I am doing wrong because my spawned bar with Text Mesh is not always straight toward the camera. Sometimes it is rotated by a certain degree. Please help!
using UnityEngine;
public class TownIndicator : MonoBehaviour
{
public Font font;
public int fontSize = 8;
public Vector3 Offset = Vector3.zero; // The offset from the character
public float health ;
private TextMesh bar;
void Start()
{
// Setup the text mesh
bar = new GameObject("HealthBar").AddComponent("TextMesh") as TextMesh;
bar.gameObject.AddComponent("MeshRenderer");
bar.gameObject.transform.parent = transform;
bar.transform.localPosition = Vector3.zero + Offset;
if(font) bar.font = font;
else bar.font = GUI.skin.font;
bar.renderer.material = font.material;
bar.characterSize = 0.25f;
bar.alignment = TextAlignment.Center;
bar.anchor = TextAnchor.MiddleCenter;
bar.fontSize = fontSize;
}
void Update()
{
// Hook up your health variable here
bar.text = ""+ health.ToString();
}
}