Create a gist now

Instantly share code, notes, and snippets.

@kihira /ScoreInfo.cs Secret
Created Nov 3, 2015

What would you like to do?
public Vector2 targetPos;
public float moveSpeed = 10; // How fast the text scrolls up
public float fadeOutTime = 2; // When to start fading out
private float timeRemaining;
void Start ()
{
timeRemaining = fadeOutTime;
}
void Update ()
{
// Move text up the screen
Vector2 pos = GetComponent<RectTransform>().anchoredPosition;
if (pos.y < targetPos.y)
{
GetComponent<RectTransform>().anchoredPosition = new Vector2(pos.x, pos.y + (float)(moveSpeed * Time.deltaTime));
}
// Begin fading out as we get near the end
if (targetPos.y - pos.y < fadeOutTime * moveSpeed)
{
timeRemaining -= Time.deltaTime;
// Fade out
Color color = gameObject.GetComponent<Text>().color;
color.a = ((timeRemaining / fadeOutTime) * 2);
gameObject.GetComponent<Text>().color = color;
}
if (timeRemaining <= 0)
{
Destroy(gameObject);
}
}
void OnDestroy()
{
GameObject.Find("Score").GetComponent<ScoreManager>().RemoveScoreInfo(gameObject);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment