Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by iKebab897 · Aug 13, 2021 at 09:31 AM · textmeshworldspace

Using TextMeshPro - Text in world space

Hi, I'm creating my first 2D topdown shooter game. The player is able to pick up weapons from the ground by standing on one and pressing E. I want a floating animated text "PICK UP [E]" to appear on the interested weapon while the player is on it.


For the text, I created a TextMeshPro - Text prefab with an animation component. I just need to place it in world space on top of the object.

 protected override void UpdateInteraction()
 {
     if (interactions.Count > 0)
     {
         if (infoText != null) Destroy(infoText.gameObject);
         var obj = interactions.Last.Value;

         infoText = Instantiate(GameManager.instance.infoTextPrefab); // Instanzia testo informazione
         infoText.text.text = obj.interactionPrompt; // Imposta il testo
         infoText.GetComponent<RectTransform>().position = obj.transform.position + new Vector3(0, 0.2f, 0); // Posiziona il testo sopra l'oggetto
         infoText.SetFollow(obj.transform); // Fai in modo che il testo segua l'oggetto
     }
     else
     {
         if (infoText != null) Destroy(infoText.gameObject);
     }
 }

The SetFollowfunction is for making sure the text follows the interested object like it if was its parent, but without rotating. Here's the definition:

 using UnityEngine;
 using TMPro;
 
 public class InfoText : MonoBehaviour
 {
     public TextMeshPro text;
 
     Transform follow;
     Vector3 positionDelta = Vector3.zero;
 
     private bool hasFollow = false;
 
     public void SetFollow(Transform obj)
     {
         follow = obj;
         positionDelta = transform.position - obj.position;
         hasFollow = true;
     }
 
     void Update()
     {
         if (follow != null)
         {
             transform.position = follow.position + positionDelta;
         }
         else if (hasFollow && follow == null)
         {
             Destroy(gameObject);
         }
     }
 }

For some reason, the text object is created far above in the scene, and I cannot move it in the editor in any way.

alt text


I think it as something to do with the text needing a world space canvas or something like that. Anybody knowing a solution?

immagine-2021-08-13-111823.png (131.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

0 Replies

· Add your reply
  • Sort: 

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

164 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 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

TextMesh always appears behind SpriteRenderer in 2D 0 Answers

How do I get rid over these things over my text? 0 Answers

Workflow for TextMesh Pro sprite glyphs? 1 Answer

TextMeshProUGUI could not be found 0 Answers

Rotate object by Parent Y and Z, but World X? 0 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