ClientServer/Client/Assets/Scripts/UI/TranslateFloatingText.cs
TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

70 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class TranslateFloatingText : MonoBehaviour
{
private TextMeshProUGUI thisText;
private float lifeTime = 1.5f;
private float startTime;
private Vector3 startingPosition;
private RectTransform thisTransform;
private const float speed = 100.0f;
private float angle;
private Vector2 randomPosition;
Color yellowColor = Color.yellow;
Color redColor = Color.red;
Color noAlpha;
private void Awake()
{
thisTransform = GetComponent<RectTransform>();
startingPosition = thisTransform.localPosition;
thisText = GetComponent<TextMeshProUGUI>();
}
private void OnEnable()
{
startTime = Time.time;
thisTransform.localPosition = startingPosition;
if(ScoreManager.Instance.isAddingPoints)
{
thisText.color = yellowColor;
noAlpha = new Color(1.0f, 1.0f, 0, 0); //yellow
angle = Random.Range(250, 350);
}
else
{
thisText.color = redColor;
noAlpha = new Color(1.0f, 0, 0, 0); //red
angle = Random.Range(150, 280);
}
angle *= Mathf.Deg2Rad;
randomPosition = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
}
void Update()
{
if (Time.time - startTime < lifeTime)
{
Color colourToUse = ScoreManager.Instance.isAddingPoints ? yellowColor : redColor;
thisText.color = Color.Lerp(colourToUse, noAlpha, (Time.time - startTime) / lifeTime);
if (thisText.color.a <= 0.05f)
{
thisText.color = noAlpha;
}
transform.Translate(randomPosition * speed * Time.deltaTime);
}
}
}