Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cmgdragon · Dec 27, 2019 at 12:19 AM · transformguilayoutdialoguelabels

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:

alt text

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):

alt text

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..?

captura3.png (7.5 kB)
captura.png (5.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cmgdragon · Dec 27, 2019 at 06:14 PM

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;
  
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

positioning gui! 2 Answers

Can I put Transform (gameObject) into GUILayout in javascript? 2 Answers

How do I modify the current transform? 2 Answers

It is not possible to invoke an expression of type 'UnityEngine.Vector3'. 1 Answer

How to make a object translate alonge an other 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges