Hi,
I’d like to place a popup-score at the position where an enemy has been destroyed. I’m developing a 2D-game in Unity3D.
So far the popup-score gets rendered inside of the canvas but the position seems to have an offset. How do I place the popup-score at the position of the destroyed enemy exactly?
This is the c#-class that fades the score:
public class FloatAndFade : MonoBehaviour {
Text text;
public float fadeDuration = 2.0f;
public float speed = 2.0f;
void Start () {
text = this.GetComponent<Text>();
StartCoroutine(Fade());
}
public IEnumerator Fade () {
float fadeSpeed = (float)1.0 / fadeDuration;
Color c = text.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadeSpeed) {
c.a = Mathf.Lerp(1, 0, t);
text.color = c;
yield return true; }
Destroy (this.gameObject);
}
void Update() {
this.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
This is the FloatScore-method that places the popup-score inside of the canvas:
void FloatScore () {
GameObject popupScore = (GameObject)Instantiate (floatScore);
popupScore.transform.position = Camera.main.WorldToScreenPoint(this.transform.position);
popupScore.transform.SetParent (canvas.transform, false);
}
These are the settings for the canvas:
This is the gameplay with the popup-score: