DNP_Target.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DamageNumbersPro.Demo
  5. {
  6. public class DNP_Target : MonoBehaviour
  7. {
  8. public Vector3 movementOffset = new Vector3(0, 0, 0);
  9. Material mat;
  10. float defaultBrightness;
  11. Coroutine hitRoutine;
  12. Coroutine flipRoutine;
  13. bool flipping;
  14. Vector3 originalPosition;
  15. void Start()
  16. {
  17. mat = GetComponent<MeshRenderer>().material;
  18. defaultBrightness = mat.GetFloat("_Brightness");
  19. flipping = false;
  20. originalPosition = transform.position;
  21. }
  22. void Update()
  23. {
  24. // Move around
  25. transform.position = originalPosition + movementOffset * Mathf.Sin(Time.time);
  26. }
  27. public void Hit()
  28. {
  29. if(hitRoutine != null)
  30. {
  31. StopCoroutine(hitRoutine);
  32. }
  33. hitRoutine = StartCoroutine(HitCoroutine());
  34. if (!flipping)
  35. {
  36. if (flipRoutine != null)
  37. {
  38. StopCoroutine(flipRoutine);
  39. }
  40. flipRoutine = StartCoroutine(FlipCoroutine());
  41. }
  42. }
  43. IEnumerator HitCoroutine()
  44. {
  45. float brightness = 1f;
  46. while( brightness < 3f)
  47. {
  48. // Glow up
  49. brightness = Mathf.Min(3, Mathf.Lerp(brightness, 3 + 0.1f, Time.deltaTime * 20f));
  50. mat.SetFloat("_Brightness", brightness);
  51. yield return null;
  52. }
  53. while(brightness > defaultBrightness)
  54. {
  55. // Glow down
  56. brightness = Mathf.Max(defaultBrightness, Mathf.Lerp(brightness, defaultBrightness - 0.1f, Time.deltaTime * 10f));
  57. mat.SetFloat("_Brightness", brightness);
  58. yield return null;
  59. }
  60. }
  61. IEnumerator FlipCoroutine()
  62. {
  63. flipping = true;
  64. float angle = 0f;
  65. while(angle < 180f)
  66. {
  67. angle = Mathf.Min(180, Mathf.Lerp(angle, 190f, Time.deltaTime * 7f));
  68. transform.eulerAngles = new Vector3(angle, 0, 0);
  69. yield return null;
  70. if(angle > 150f)
  71. {
  72. flipping = false;
  73. }
  74. }
  75. }
  76. }
  77. }