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