Making label following a moving character?

Hello,

I have been working on the dialogue system of my first game,

it consists of two methods, one for printing the text and other for determining the position of that text in the screen,

my intention is to have a label following the character in front of it, just like a speech square

these are the methods:

public void PrintSentence(string sentence, Character character) {

    Vector2 sentenceSize = style.CalcSize(new GUIContent(sentence));
    List<float> pos = new List<float>();

    pos = dUtil.LabelFollow(character, sentenceSize);

    GUILayout.BeginArea(new Rect(pos[0], pos[1], sentenceSize.x, limitWidth));

    GUILayout.Label(new GUIContent(sentence), style, GUILayout.MaxWidth(limitWidth));

    GUILayout.EndArea();

}

public List<float> LabelFollow(Character character, Vector2 sentenceSize) {

    //this is the gameobject position that I want the label to follow
    Transform labelPos = GetCharacter(character).transform.GetChild(0).transform.position;

    Vector2 labelSize = labelPos.InverseTransformPoint(sentenceSize);

    //if the character moves left
    if (labelPos.parent.transform.localScale.x < 0) {
        pos = new Vector2(pos.x - labelSize.x, pos.y);
    } 

    pos = Camera.main.WorldToScreenPoint(pos);

    //storing coordinates
    List<float> arr = new List<float>();
    arr.Add(pos.x);
    arr.Add(Screen.height - pos.y);

    return arr;

}

by the moment it works fine and I get what expected:

150507-captura.png

the red mark is an empty gameobject for defining the position of the label

the problem occurs when the character turns left (I make this scaling the x axis to -1):

And like the label is not part of the character, it doesn’t scales

I don’t want to have it above because sometimes the sentence is too long and covers the character and also for fitting with the game aesthetic.

Then I thought I could move the label to the left on the x axis
so I added (as shown before):

//if the character moves left
if (labelPos.parent.transform.localScale.x < 0) {
    pos = new Vector2(pos.x - labelSize.x, pos.y);
} 

unfortunately this time the label dissapears of the screen

Then I tried to modify the position manually to make it fit correctly, but then it doesn’t works in other screen resolutions

everything I have been trying make the label dissapear from the screen or not place it in the correct position,

Any clues on how to fix this…?

Well finally I figured it out by myself,

I just needed to modify the position of the label in the PrintSentence function instead of doing it in the FollowLabel. I’m sharing the result here:

 public void PrintSentence(string sentence, Character character) {
 
     Vector2 sentenceSize = style.CalcSize(new GUIContent(sentence));
     List<float> pos = new List<float>();
 
     pos = dUtil.LabelFollow(character);

     float separation = sentenceSize.x >= limitWidth ? sentenceSize.x / 2 : sentenceSize.x;

     //check if the character has turned left
     GUILayout.BeginArea(new Rect(pos[2] == 1 ? pos[0] - separation : pos[0]

          , pos[1] , sentenceSize.x, limitWidth));
 
     GUILayout.Label(new GUIContent(sentence), style, GUILayout.MaxWidth(limitWidth));
 
     GUILayout.EndArea();
 
 }
 public List<float> LabelFollow(Character character) {
 
     //this is the gameobject position that I want the label to follow
     Transform labelPos = GetCharacter(character).transform.GetChild(0).transform.position;
     
     float left = 0;

     //if the character moves left
     if (labelPos.parent.transform.localScale.x < 0) {
         left = 1;
     } 
     pos = Camera.main.WorldToScreenPoint(pos);
 
     //storing coordinates
     List<float> arr = new List<float>();
     arr.Add(pos.x);
     arr.Add(Screen.height - pos.y);
     arr.Add(left);
 
     return arr;
 
 }