DNP_FallingCube.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace DamageNumbersPro.Demo
  6. {
  7. public class DNP_FallingCube : MonoBehaviour
  8. {
  9. public float fallSpeed = 100f;
  10. RectTransform rect;
  11. Image image;
  12. float currentSpeed;
  13. bool isBroken;
  14. void Start()
  15. {
  16. rect = GetComponent<RectTransform>();
  17. image = GetComponent<Image>();
  18. float speedAndSize = Random.value * 0.4f + 0.8f;
  19. currentSpeed = fallSpeed * speedAndSize;
  20. transform.localScale = Vector3.one * speedAndSize;
  21. rect.anchoredPosition3D = new Vector3(Random.value * 560 - 280f, 260f, 0);
  22. rect.localEulerAngles = new Vector3(0, 0, Random.value * 360f);
  23. image.color = Color.Lerp(new Color(0.5f, 0.5f, 0.5f), image.color, speedAndSize - 0.2f);
  24. }
  25. void FixedUpdate()
  26. {
  27. rect.anchoredPosition += new Vector2(0, currentSpeed * Time.fixedDeltaTime);
  28. if(isBroken)
  29. {
  30. currentSpeed = Mathf.Lerp(currentSpeed, 0, Time.fixedDeltaTime * 3f);
  31. transform.localScale += Vector3.one * Time.fixedDeltaTime * 3f;
  32. Color color = image.color;
  33. color.a -= Time.fixedDeltaTime * 5f;
  34. if(color.a <= 0)
  35. {
  36. Destroy(gameObject);
  37. }
  38. else
  39. {
  40. image.color = color;
  41. }
  42. }
  43. if(rect.anchoredPosition.y < -270f)
  44. {
  45. Destroy(gameObject);
  46. }
  47. }
  48. public void Break()
  49. {
  50. isBroken = true;
  51. image.raycastTarget = false;
  52. }
  53. }
  54. }