Blipples

FloatingText - nob

Feb 15th, 2025 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. /// <summary>
  2. /// Controls the floating text behavior.
  3. /// </summary>
  4. public class FloatingText : NetworkBehaviour
  5. {
  6. /// <summary>
  7. /// The TextMeshPro component used to display the floating text.
  8. /// </summary>
  9. [SerializeField] private TextMeshPro floatingText;
  10.  
  11. /// <summary>
  12. /// CountdownTimer fo
  13. /// </summary>
  14. private CountdownTimer _countdownTimer;
  15.  
  16. /// <summary>
  17. /// The time after which the text will be destroyed.
  18. /// </summary>
  19. public float textDestroyTimer = 2f;
  20.  
  21. /// <summary>
  22. /// The speed at which the text moves upwards.
  23. /// </summary>
  24. public float textSpeed = .01f;
  25.  
  26. public override void OnStartNetwork()
  27. {
  28. base.OnStartNetwork();
  29.  
  30. floatingText = GetComponent<TextMeshPro>();
  31. _countdownTimer = new CountdownTimer(textDestroyTimer);
  32. _countdownTimer.Start();
  33. }
  34.  
  35. private void Update()
  36. {
  37. // Tick the timer if it isn't finished
  38. if (!_countdownTimer.IsFinished())
  39. {
  40. _countdownTimer.Tick(Time.deltaTime);
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// Update is called once per frame.
  46. /// </summary>
  47. private void FixedUpdate()
  48. {
  49. transform.Translate(Vector2.up * (textSpeed * Time.deltaTime));
  50.  
  51. if (_countdownTimer.IsFinished())
  52. {
  53. SpawnManager.Instance.RPC_DespawnObject(base.NetworkObject);
  54. }
  55. }
  56.  
  57. /// <summary>
  58. /// Sets the text and color of the floating text.
  59. /// </summary>
  60. /// <param name="text">The text to display.</param>
  61. /// <param name="color">The color of the text.</param>
  62. [ServerRpc(RequireOwnership = false)]
  63. public void RPC_SetText(string text, Color color)
  64. {
  65. SetText(text, color);
  66. }
  67.  
  68. [ObserversRpc]
  69. private void SetText(string text, Color color)
  70. {
  71. if (floatingText == null)
  72. {
  73. Debug.LogWarning("FloatingText component is not assigned.");
  74. return;
  75. }
  76.  
  77. floatingText.text = text;
  78. floatingText.color = color;
  79. }
  80.  
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment