- Home /
Smart way to add border to a text mesh in a 2d environment
Hello! I am making a text mesh in my game project which is a damage number, it looks very good at the moment. But, I would love to add a black border around the text mesh, to add to the pixely style of the game. I'm thinking that I would just instantiate a textmesh child object of the text mesh that is slightly bigger and being behind the original text mesh object. Any smart way to do this?
Here is my code of assigning the textmesh value:
public float hpLost;
void TakeDamage() {
TextMesh text = (TextMesh)thing.GetComponent(typeof(TextMesh));
text.text = "-" + hpLost.ToString("F1");
thing.rigidbody2D.AddRelativeForce(randomDirection);
}
//hpLost is assigned damageDealt from another script.
If you think there is lacking some vital information, please just ask, but there's not much more to the textmesh :)
This solution is working fine for what it is meant to do, which is to create a text mesh with the value I assign it, which is hpLost (amount of health points lost). The new solution I am looking for, is to add a black border around this text-mesh. It would look much more esthetically pleasing in my game.
Changing the size of a text-mesh works on the individual letters of the text mesh, so, for me to be able to create a pseudo black border looking thing, I could add 5 text-meshes with black color, just behind the original text-mesh with small difference in placement in the x and y axis. This would give the illusion of a black border. I believe this will work (having played around with it a while), but I don't like the thought of instantiating 6 objects every time this method gets called.
Answer by Stealthygolem · Nov 27, 2014 at 09:15 PM
I found a solution that I consider is temporary, but it does look much better, and I only instantiate two more objects.
This is how my method now looks like. I added two other duplicates of the first textmesh and changed their color to black, and as you can see in the script I offset the Z-value a bit, to add to the illusion that it has a border. These two only add borders for the left and right side of the mesh in theory. But, it does look quite good, and I could make it look even better by just adding more some more if I would find it desirable. The consideration would be performance, of course!
void TakeDamage() {
objectSpeed = 60f;
Vector2 randomDirection = Random.insideUnitCircle * objectSpeed;
GameObject dicks = Instantiate(textMeshObject,transform.position,Quaternion.identity) as GameObject;
TextMesh text = (TextMesh)dicks.GetComponent(typeof(TextMesh));
text.text = "-" + hpLost.ToString("F1");
dicks.rigidbody2D.AddRelativeForce(randomDirection);
GameObject dicks2 = Instantiate(textMeshObject2, new Vector3 (transform.position.x+0.02f,
transform.position.y,
transform.position.z),Quaternion.identity) as GameObject;
TextMesh text2 = (TextMesh)dicks2.GetComponent(typeof(TextMesh));
text2.text = "-" + hpLost.ToString("F1");
dicks2.rigidbody2D.AddRelativeForce(randomDirection);
GameObject dicks3 = Instantiate(textMeshObject3, new Vector3 (transform.position.x-0.02f,
transform.position.y,
transform.position.z),Quaternion.identity) as GameObject;
TextMesh text3 = (TextMesh)dicks3.GetComponent(typeof(TextMesh));
text3.text = "-" + hpLost.ToString("F1");
dicks3.rigidbody2D.AddRelativeForce(randomDirection);
}
Any critique on this or a better idea is still rather highly appreciated.
Your answer
Follow this Question
Related Questions
How do I get the global position 2 Answers
Network.Instantiate fired from client AND server 1 Answer
UnityException: Transform child out of bounds. 1 Answer
Instantiate UI object as child of panel relative to panel's location 1 Answer
How can I make a game object follow an instantiated game object? 1 Answer