- Home /
Is it possible to display a variable relative to an object at runtime e.g. health in numbers above an enemy?
What I'm asking was really simple for me to do in other game engines but I haven't really gotten around to get it to work in Unity. I'm trying to get all the enemies in my game to display the value of their health variable on top of them at runtime. There can be multiple instances of the same enemy in the game at the same time and most of these objects get instantiated during runtime.
The code I that I have for the enemy so far:
using UnityEngine;
using System.Collections;
public class CreatureScript : MonoBehaviour
{
public int dir = 0;
public int changetime = -1;
private int timer = 0;
public int mspeed = 2;
public int side = 0;
public int health = 2;
public int attack = 1;
CreatureScript creatureScript;
BulletScript bulletScript;
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "wall") //Collision with wall
{
mspeed = mspeed * -1;
}
if(other.tag == "creature") //Collision with creature
{
mspeed = mspeed * -1;
CreatureScript creatureScript = other.GetComponent<CreatureScript>();
if(side != creatureScript.side)
{
health -= creatureScript.attack;
if(health < 1)
{
Destroy(gameObject);
}
}
}
}
void FixedUpdate ()
{
if(timer == changetime) //set random direction
{
timer = 0;
dir = Random.Range(0,4);
}
timer = timer + 1;
//Change direction
if(dir == 0)
transform.Translate (new Vector3(mspeed*Time.deltaTime,0*Time.deltaTime,0));
if(dir == 1)
transform.Translate (new Vector3(-mspeed*Time.deltaTime,0*Time.deltaTime,0));
if(dir == 2)
transform.Translate (new Vector3(0*Time.deltaTime,mspeed*Time.deltaTime,0));
if(dir == 3)
transform.Translate (new Vector3(0*Time.deltaTime,-mspeed*Time.deltaTime,0));
}
}
I already tried playing around with the new UI System but haven't really found an option that makes this work yet.
Thanks for your answers in advance.
Answer by cjdev · Aug 27, 2015 at 07:46 PM
Yes you can position UI elements on screen based on your GameObject's world coordinates by transforming them into screen coordinates. Here's an example, although it's done entirely in script so I would recommend making a text prefab and loading it from the Resources folder unless you're comfortable with C#:
//using UnityEngine.UI;
void Start()
{
//Make sure there's a Canvas UI object in your scene named 'Canvas'
//Note: it is much easier to make a prefab and load it from resources than to do this
// vvvvvvvvv
canv = GameObject.Find("Canvas").GetComponent<Canvas>();
canvTransform = canv.GetComponent<RectTransform>();
canv.renderMode = RenderMode.ScreenSpaceOverlay;
GameObject txtObj = new GameObject("Text");
txt = txtObj.AddComponent<Text>();
txtObj.transform.SetParent(canv.transform, false);
txt.font = Font.CreateDynamicFontFromOSFont("Arial", 16);
txt.alignment = TextAnchor.UpperCenter;
// ^^^^^^^^^
txt.text = health.ToString();
}
void Update()
{
Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, transform.position);
txt.rectTransform.anchoredPosition = screenPoint - canvTransform.sizeDelta / 2f;
txt.text = health.ToString();
}
Thanks a lot for the answer. I had some trouble implementing the code at first but in the end it all worked now. For other people who stumble upon this question make sure to add these 3 lines of code, to the initialisation(I'm not sure what exactly you call this part yet) at the start of the class: Canvas canv; RectTransform canvTransform; Text txt; Also make sure that you are using the UnityEngine.UI, as cjdev stated in his script.
Your answer
Follow this Question
Related Questions
How to display EULA from Doc in Unity? 0 Answers
Change the game window built 1 Answer
When I try to increment a variable by 1, why does it add 10? 1 Answer
Reference other scrips in prefab 1 Answer
Score & High Score logic doesn't work 2 Answers