Micro__6

CardCollector

May 25th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class CardCollector : MonoBehaviour
  6. {
  7. public Transform player; // Assign the player transform in the inspector
  8. public float detectionRange = 5f;
  9. public KeyCode collectKey = KeyCode.F;
  10. public GameObject book; // Assign the book game object in the inspector
  11. public string cardSlotPrefix = "Slot"; // Prefix of the specific card slots (e.g., Slot001, Slot002, etc.)
  12.  
  13. private HashSet<int> collectedCards = new HashSet<int>(); // Stores collected card IDs
  14.  
  15. private void Update()
  16. {
  17. GameObject[] cards = GameObject.FindGameObjectsWithTag("Card");
  18.  
  19. foreach (GameObject card in cards)
  20. {
  21. float distance = Vector3.Distance(player.position, card.transform.position);
  22.  
  23. // Log if the card is within range
  24. if (distance <= detectionRange)
  25. {
  26. Debug.Log("Card detected: " + card.name); // Log this message
  27.  
  28. if (Input.GetKeyDown(collectKey))
  29. {
  30. CollectCard(card);
  31. }
  32. }
  33. }
  34. }
  35.  
  36. private Transform FindSlotRecursive(Transform parent, string slotName)
  37. {
  38. foreach (Transform child in parent)
  39. {
  40. if (child.name == slotName)
  41. {
  42. return child;
  43. }
  44. Transform found = FindSlotRecursive(child, slotName);
  45. if (found != null)
  46. {
  47. return found;
  48. }
  49. }
  50. return null;
  51. }
  52.  
  53. private Transform FindEmptySlot()
  54. {
  55. foreach (Transform page in book.transform)
  56. {
  57. foreach (Transform slot in page)
  58. {
  59. if (slot.childCount == 0) // If the slot is empty
  60. {
  61. return slot;
  62. }
  63. }
  64. }
  65. return null;
  66. }
  67.  
  68. private void CollectCard(GameObject card)
  69. {
  70. Card cardComponent = card.GetComponent<Card>();
  71. if (cardComponent == null)
  72. {
  73. Debug.LogError("Card component is missing on " + card.name);
  74. return;
  75. }
  76.  
  77. int cardID = cardComponent.CardID;
  78.  
  79. if (collectedCards.Contains(cardID))
  80. {
  81. Debug.Log("Card ID " + cardID + " is already in the book.");
  82. return;
  83. }
  84.  
  85. if (book == null)
  86. {
  87. Debug.LogError("Book GameObject is not assigned.");
  88. return;
  89. }
  90.  
  91. // Find the correct slot based on the card ID
  92. string slotName = cardSlotPrefix + cardID.ToString("D3");
  93. Transform slot = FindSlotRecursive(book.transform, slotName);
  94.  
  95. if (slot == null)
  96. {
  97. slot = FindEmptySlot(); // Try finding a generic empty slot
  98. }
  99.  
  100. if (slot != null)
  101. {
  102. // Move the card to the slot
  103. card.transform.SetParent(slot, false); // Keeps world position when parenting
  104. card.transform.localPosition = Vector3.zero; // Make sure the card is centered in the slot
  105. card.transform.localRotation = Quaternion.identity; // Reset rotation
  106.  
  107. // Ensure scale is correct
  108. Vector3 slotSize = slot.GetComponent<Renderer>().bounds.size;
  109. Vector3 cardSize = card.GetComponent<Renderer>().bounds.size;
  110.  
  111. float scaleFactor = Mathf.Min(slotSize.x / cardSize.x, slotSize.y / cardSize.y, slotSize.z / cardSize.z);
  112. card.transform.localScale = Vector3.one * scaleFactor;
  113.  
  114. // Ensure card is visible
  115. if (card.TryGetComponent<Renderer>(out Renderer cardRenderer))
  116. {
  117. cardRenderer.enabled = true; // Ensure card is visible
  118. }
  119. else
  120. {
  121. Debug.LogWarning("Renderer component is missing on " + card.name);
  122. }
  123.  
  124. // Hide card on pickup and show inside book
  125. if (card.TryGetComponent<Renderer>(out Renderer pickupRenderer))
  126. {
  127. pickupRenderer.enabled = false; // Hide when picked up
  128. StartCoroutine(ShowCard(pickupRenderer));
  129. }
  130.  
  131. // Store the collected card ID
  132. collectedCards.Add(cardID);
  133. Debug.Log("Card collected and placed in slot: " + slotName);
  134. }
  135. else
  136. {
  137. Debug.Log("No available slot. Book might be full.");
  138. }
  139. }
  140.  
  141.  
  142. private IEnumerator ShowCard(Renderer cardRenderer)
  143. {
  144. yield return new WaitForSeconds(0.2f);
  145. cardRenderer.enabled = true;
  146. }
  147. }
Add Comment
Please, Sign In to add comment