using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class DamageTextController : MonoBehaviour { private TextMeshPro text; [SerializeField] public float holdTime = 1.0f; [SerializeField] public float fadeTime = 1.0f; [SerializeField] public float shakeTime = 1.0f; [SerializeField] public float shakeTick = 0.05f; [SerializeField] public float shakeTransposeX = 0.05f; [SerializeField] public float shakeTransposeY = 0f; private bool shouldShake = true; private Vector3 originalPosition; private void Awake() { text = transform.GetComponent(); originalPosition = transform.position; } public void Setup(int amount) { text.SetText(amount.ToString()); } // Start is called before the first frame update void Start() { StartCoroutine("Shake"); Invoke("StartFadeOut", holdTime); shouldShake = true; Invoke("StopShake", shakeTime); // Appear over other damage text. transform.SetAsLastSibling(); } void StopShake() { shouldShake = false; } IEnumerator Shake() { while (shouldShake) { transform.position = new Vector2(originalPosition.x + shakeTransposeX * Random.Range(-1.0f, 1.0f), originalPosition.y + shakeTransposeY * Random.Range(-1.0f, 1.0f)); yield return new WaitForSeconds(shakeTick); } transform.position = originalPosition; } public void StartFadeOut() { StartCoroutine("FadeOut"); } IEnumerator FadeOut() { for (float i = fadeTime; i >= 0; i -= Time.deltaTime) { text.alpha = i / fadeTime; yield return null; } Destroy(gameObject); } }