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 /
  • Help Room /
avatar image
0
Question by Rembo4Fight · Feb 17, 2018 at 07:28 PM · textworldtoscreenpointappearcollectible

Word Space Text Appearing after Collecting the Object

Hello, I'm making the system, where the Player is collecting the collectible object and after that text is appearing on the collectibles position and shows some value of collectible item So I created a script where is everything I think i need, but nothing is happening Please check it and if you know what to add tell me :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class ScoreManager : MonoBehaviour {
 
     Animator anim;
 
     public static int score;
 
 
     private float m_smoothScore;
     private float m_smoothScoreVelocity;
     private int m_displayedScore = -1;
 
     TextMeshProUGUI text;
 
     //TextMeshPro worldText;
 
     public static bool collected;
 
     public float min;
     public float max;
     public float t;
 
     [SerializeField] private Canvas uiCanvas = null;
     [SerializeField] private TextMeshPro scoreTextPrefab = null;
 
     void Awake()
     {
         text = GetComponent<TextMeshProUGUI> ();
         score = 0;
     }
 
     void Start()
     {
         anim = GetComponent<Animator> ();
         collected = false;
     }
 
     void Update () {
         //smooth score animation
         m_smoothScore = Mathf.SmoothDamp(m_smoothScore,(float)score,ref m_smoothScoreVelocity, 0.2f, Mathf.Infinity, Time.deltaTime * 1.2f);
 
         //display the text
         int toDisplay = (int)Mathf.Round(m_smoothScore);
         if (toDisplay != m_displayedScore) 
         {
             m_displayedScore = toDisplay;
             text.text = toDisplay + " PTS";
         }
 
         if (score > 0) 
         {
             anim.SetBool ("Points", true);
         }
         if (collected == true) {
             t = Time.time;
             text.fontSize = Mathf.Lerp (min, max, t);
             collected = false;
         } else {
             t = Time.time;
             text.fontSize = Mathf.Lerp (max, min, t);
         }
     } 
 
     public void OnCollecting(Vector3 collectiblePosition)
     {
         TextMeshPro worldText = Instantiate (scoreTextPrefab);
         worldText.transform.SetParent (uiCanvas.transform);
         worldText.transform.position = Camera.main.WorldToScreenPoint (collectiblePosition + Vector3.up);
     }
 }
 
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 Glurth · Feb 17, 2018 at 09:17 PM

Do you need to use a canvas? That is generally for laying out your UI, not for showing text in world space. If possible, I would eliminate:

 worldText.transform.SetParent (uiCanvas.transform);

or assign it to a non-canvas parent.

But I think the main issue is:

 worldText.transform.position = Camera.main.WorldToScreenPoint (collectiblePosition + Vector3.up);

should probably be:

 worldText.transform.position = collectiblePosition + Vector3.up;

Since you want it at a 3d location in space. If you want the text to always be a particular distance from the camera, so it stays the same size regardless of how far away in 3d space it is: THEN you can use WorldToScreenPoint AND pass that result, and a fixed distance value as the z-value of the vector3 parameter), to ScreenToWorldPoint, to generate the 3d world-space position to place it.

e.g.

 Vector3 textPos = Camera.main.WorldToScreenPoint (collectiblePosition + Vector3.up);
 textPos.z= fixedDistanceForAllText;
 worldText.transform.position = Camera.main.ScreenToWorldPoint(textPos);


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 Rembo4Fight · Feb 17, 2018 at 10:16 PM 0
Share

Thank you for your answer! I'm making 2D game, I'm sorry I maybe confuse you. So what I want, just to make the text of my UI to have a position of my collectible object, after player collected it. So the Z value is will be 0. The main problem, that everything I tried to do is not working with moving my textPrefab to the place of collectible object. It is because "collectiblePosition" just doesn't mean anything? I'm just trying to resolve this. :) What your opinion how to achieve this? If you can, show please some script example.

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

78 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

Related Questions

How do i appear text after a specific time? 0 Answers

How do I make both an Image and Text appear when a button is clicked? 0 Answers

OnGUI behind UI? 0 Answers

How can i let a sign appear when collected a certain amount of orbs ? 2 Answers

How do You add a Scrollbar in a textbox C# code 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