- Home /
Question by
Rembo4Fight · Feb 18, 2018 at 09:41 AM ·
scripting problemvector2positioning
Put the text on Collectibles position !
Hi, I tried to make it about a week, and nothing helps on my own.
So I'm making a 2D game, where the player collecting collectibles, and I want to make the text appearing on the position where was the collectibles item.
So I added spawnX and spawnY variable to my Collectibles script to know the position of collectible item.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectibles : MonoBehaviour {
public int scoreValue;
public bool alreadyScored;
public float spawnX, spawnY;
void Start()
{
spawnX = transform.position.x;
spawnY = transform.position.y;
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.layer == LayerMask.NameToLayer ("Player")) {
if (alreadyScored)
return;
alreadyScored = true;
ScoreManager.score += scoreValue;
ScoreManager.collected = true;
} else {
ScoreManager.collected = false;
}
}
}
And created the ScoreManager script, which is collecting all the data.
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 TMPtext;
public static bool collected;
public float min;
public float max;
public float t;
void Awake()
{
TMPtext = 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;
TMPtext.text = toDisplay + " PTS";
}
if (score > 0)
{
anim.SetBool ("Points", true);
}
if (collected == true) {
t = Time.time;
TMPtext.fontSize = Mathf.Lerp (min, max, t);
collected = false;
} else {
t = Time.time;
TMPtext.fontSize = Mathf.Lerp (max, min, t);
}
}
}
So what exactly I need to make to have the (prefab) text, which will be appearing on the position of the collectible item. I know that collectible item will be destroy, but spawnX and spawnY will send the info about positioning sooner then it will be destroyed.
Thank you if you read this all, I hope this will resolve. :)
Comment
Your answer