• 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 AdamRV · Sep 25, 2012 at 09:08 PM · guitextsize

3D Text Size Relative To Screen?

Hello.

I recently added some 3D text to my scene to follow the top of the player. This was done so the Player's name wouldn't appear when he was behind a building.

I got that to successfully work, but the only problem I'm having now that I moved it into 3D space, is that the text doesn't keep the same size relative to the screen size. The further you go away, the the harder it is to notice the player's name. Anyone know how to keep the size consistent relative to the screen?

Thank you

  • Adam

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Ivan-Carmosino · Dec 21, 2016 at 08:20 AM

You could set the scale of the textGameObject in according to the distance from the camera in this way:

 using UnityEngine;
 
 public class ScaleMeshText : MonoBehaviour {
     [Tooltip("Distance of the MeshObject with no calculateScale")]
     public float distance = 7.0f;
     Vector3 startScale;
 
     void Start () {
         startScale = transform.localScale;
     }
     
     void Update () {
         scale();
     }
 
     void scale()
     {
         float dist = Vector3.Distance(Camera.main.transform.position, transform.position);
         Vector3 newScale = startScale * (dist / distance);
         transform.localScale = newScale;
     } 
 }
Comment
Add comment · Show 1 · 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
avatar image Banaaani · 16 minutes ago 0
Share

Thanks! Very simple and effective, easy to understand solution.

avatar image
-1

Answer by drawcode · Sep 25, 2012 at 09:49 PM

You could set the scale of the object according to the distance from the camera. You will have to adjust the values/settings but basically just use Vector3.Distance on the camera transform and the 3d text transform.

 public class Example : MonoBehaviour {
     public Transform cameraTransform;
     void Example() {
         if (other) {
             float dist = Vector3.Distance(cameraTransform.position, transform.position);
             print("Distance to other: " + dist);
             Vector3 newScale = transform.localScale;
             // adjust local scale based on distance
             transform.localScale = newScale;
         }
     }
 }

You can also combine this with http://wiki.unity3d.com/index.php?title=CameraFacingBillboard for other types not just 3d text.

Comment
Add comment · Show 1 · 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
avatar image oliver-jones · Sep 15, 2013 at 12:09 PM 0
Share

This doesn't do anything - you're simply applying the current scale ... to the current scale, the 'dist' is never used.

avatar image
0

Answer by Eric5h5 · Sep 26, 2012 at 01:03 AM

Use a GUIText object instead of 3D text. (For example.)

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
avatar image
0

Answer by ridlr · Jun 12, 2020 at 08:34 PM

Obviously this is an old thread, but just thought I would help anybody else looking for an answer. The first thing you must do is pick a default font size and relative default distance, like so:

 float defaultDistance = 4f;
 float defaultSize = 40f;


Then use something like this to resize it:

 float dist = Vector3.Distance(playerCamera.position, transform.position);
 textMesh.fontSize = Mathf.RoundToInt(defaultSize * Mathf.Sqrt(dist / defaultDistance));

The square root is key here, because otherwise the text will scale disproportionately to the camera's distance.

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

15 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

Related Questions

Changing a GUILabel text SIZE 3 Answers

Unity 4.6.1 Font Size Cannot Go Over 25 6 Answers

Relate font size to screen size in GUISkin 2 Answers

how to scale and position guiText in android 1 Answer

Changing the size of a GUI label 1 Answer


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