Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Text at Location in 2D world space

Discussion in 'UGUI & TextMesh Pro' started by JymWythawhy, Apr 16, 2016.

  1. JymWythawhy

    JymWythawhy

    Joined:
    Jan 18, 2015
    Posts:
    25
    I am trying to create a system to make damage numbers pop up over my units' heads in a top down rogue-like game, and what I would like to do is have a Damage Text Controller that has a pool of damage text objects that it can make appear at any point called, but I can't figure out how to get Text objects, which are on a Canvas, to correspond to game space coordinates, even if the Canvas is set to World Space.

    For example, if I had a unit at space (45,35), I could call DamageText.Instance.SpawnDamageText(45,35) and it would move and make visible a text prefab to that point and make it do its thing before disappearing and being requeued in the pool of text objects, ready to be spawned somewhere else.

    Is this even possible/workable? I feel like it should be easy enough to spawn text objects at a location like I can with sprites, but because the new UI system seems divorced from the coordinate system of the rest of the game engine (which has benefits, I know), I can't find a simple way of doing it.

    Alternately I think I could have every unit have its own canvas it drags around with it and have the damage text controller parent one of its pooled text objects to the unit's canvas for that damage notification, and then un-parent it after the notification is done, which is probably what I am going to have to do... It just seems like I should be able to do it this other way.

    Am I missing something?

    Thank you.
     
  2. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    What you want to do is to take a point from world space and convert it into canvas space, correct?

    If that's the case then you'll need to create a function to convert world space coords:
    Code (CSharp):
    1.    
    2. public static Vector2 WorldToCanvas(RectTransform canv, Vector3 pos)
    3.     {
    4.      
    5. //Please note that this will only work if the element you're positioning is either a direct child of the main canvas or the child of a transform that stretches to the full extents of the canvas. Otherwise you have to take the offsets of all parent RectTransforms into account.
    6.         if (Camera.main == null)
    7.             return Vector2.zero;
    8.  
    9. //This part is a bit complicated, but you have to make sure that you adjust your coordinates by the size of the canvas.
    10.         Vector2 cPos = new Vector2(((Camera.main.WorldToViewportPoint(pos).x * canv.sizeDelta.x) - (canv.sizeDelta.x * .5f)), ((Camera.main.WorldToViewportPoint(pos).y * canv.sizeDelta.y) - (canv.sizeDelta.y * .5f)));
    11.  
    12.  
    13.         return cPos;
    14.     }
    15.  
    From that point there are a few easy steps you can take to convert the coordinates:

    1. Get the coordinates in world space. I'm assuming you have this already, but it would ideally be a point above the unit's head. (Can be an empty GameObject).
    2. Use your new function to convert your coordinates like so:
    Code (CSharp):
    1. //Main Canvas here is the RectTransform component of the canvas I'm trying to convert coordinates to
    2. //CurObject would be the GameObject reference to the point you want to anchor the text.
    3. Vector2 nVec = WorldToCanvas(MainCanvas, CurObject.transform.position);
    3. Set the new vector as your Text object's anchored position.

    Hope that helps you out a bit. Let me know if you have questions on that or need anything explained further.
     
    EnergYCustom likes this.
  3. JymWythawhy

    JymWythawhy

    Joined:
    Jan 18, 2015
    Posts:
    25
    Thank you for your help! It worked great.