CardPopup2D.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /// <summary>
  2. /// Credit - ryanslikesocool
  3. /// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
  4. /// </summary>
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. namespace UnityEngine.UI.Extensions
  9. {
  10. [RequireComponent(typeof(Rigidbody))]
  11. public class CardPopup2D : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private float rotationSpeed = 1f;
  15. [SerializeField]
  16. private float centeringSpeed = 4f;
  17. [SerializeField]
  18. private bool singleScene = false;
  19. private Rigidbody rbody;
  20. private bool isFalling;
  21. private Vector3 cardFallRotation;
  22. private bool fallToZero;
  23. private float startZPos;
  24. void Start()
  25. {
  26. rbody = GetComponent<Rigidbody>();
  27. rbody.useGravity = false;
  28. startZPos = transform.position.z;
  29. }
  30. void Update()
  31. {
  32. if (isFalling)
  33. {
  34. transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(cardFallRotation), Time.deltaTime * rotationSpeed);
  35. }
  36. ///This conditional makes the popup fall nicely into place.
  37. if (fallToZero)
  38. {
  39. transform.position = Vector3.Lerp(transform.position, new Vector3(0, 0, startZPos), Time.deltaTime * centeringSpeed);
  40. transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(Vector3.zero), Time.deltaTime * centeringSpeed);
  41. if (Vector3.Distance(transform.position, new Vector3(0, 0, startZPos)) < 0.0025f)
  42. {
  43. transform.position = new Vector3(0, 0, startZPos);
  44. fallToZero = false;
  45. }
  46. }
  47. ///This is totally unnecessary.
  48. if (transform.position.y < -4)
  49. {
  50. isFalling = false;
  51. rbody.useGravity = false;
  52. rbody.velocity = Vector3.zero;
  53. transform.position = new Vector3(0, 8, startZPos);
  54. if (singleScene)
  55. {
  56. CardEnter();
  57. }
  58. }
  59. }
  60. public void CardEnter()
  61. {
  62. fallToZero = true;
  63. }
  64. ///A negative fallRotation will result in the card turning clockwise, while a positive fallRotation makes the card turn counterclockwise.
  65. public void CardFallAway(float fallRotation)
  66. {
  67. rbody.useGravity = true;
  68. isFalling = true;
  69. cardFallRotation = new Vector3(0, 0, fallRotation);
  70. }
  71. }
  72. }