I'm making an isometric-ish 2d game in Unity. I want to display text above a sprite during certain conditions. For this, I'm using a TextMesh instantiated as a child of the sprite.

In local space, this obviously means that when I rotate a sprite, its children rotate along with it. From the player's perspective, the label now appears to the side of the sprite, rather than staying above it.

I can always calculate the world space position of the TextMesh during LateUpdate(), but a) that solution requires a designer to know that a script will alter the local transform, and b) things built into Unity have more eyeballs on them, so I'd rather use that.

Is there a mechanism built into Unity such that I can set a child's transform in the inspector to e.g. position.y = 0.4 and have it render 0.4 units above the parent in world space? Edit: Additionally, I want it to follow the parent as it moves, so just calculating once on Instantiate() isn't quite what I want either.

  • \$\begingroup\$ In the inspector? No. You'd have to write a custom script. \$\endgroup\$ – Draco18s no longer trusts SE Aug 15 '17 at 1:32
  • \$\begingroup\$ Ah, thanks for the confirmation. Would using UI text to accomplish this work, or is this TextMesh with script approach my best bet? \$\endgroup\$ – Justin Aug 15 '17 at 3:30
  • \$\begingroup\$ UI text would probably be just as awkward. Just create a script with a public Vector3 that it uses to update the position of its game object. \$\endgroup\$ – Draco18s no longer trusts SE Aug 15 '17 at 3:43
  • \$\begingroup\$ Thanks. If you want to post that as an answer, I'd be glad to accept it. \$\endgroup\$ – Justin Aug 15 '17 at 15:25

Create your own component to do this

Create a MonoBehaviour component script that exposes a public Vector3 that is then used to update the position of the object in WorldSpace instead of local.

  • \$\begingroup\$ Thanks! I went with code that copies the transform.position to a private member variable on Awake() so that a designer can use the inspector to set the values without the script overwriting them. \$\endgroup\$ – Justin Aug 15 '17 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.