Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class CardCollector : MonoBehaviour
- {
- public Transform player; // Assign the player transform in the inspector
- public float detectionRange = 5f;
- public KeyCode collectKey = KeyCode.F;
- public GameObject book; // Assign the book game object in the inspector
- public string cardSlotPrefix = "Slot"; // Prefix of the specific card slots (e.g., Slot001, Slot002, etc.)
- private HashSet<int> collectedCards = new HashSet<int>(); // Stores collected card IDs
- private void Update()
- {
- GameObject[] cards = GameObject.FindGameObjectsWithTag("Card");
- foreach (GameObject card in cards)
- {
- float distance = Vector3.Distance(player.position, card.transform.position);
- // Log if the card is within range
- if (distance <= detectionRange)
- {
- Debug.Log("Card detected: " + card.name); // Log this message
- if (Input.GetKeyDown(collectKey))
- {
- CollectCard(card);
- }
- }
- }
- }
- private Transform FindSlotRecursive(Transform parent, string slotName)
- {
- foreach (Transform child in parent)
- {
- if (child.name == slotName)
- {
- return child;
- }
- Transform found = FindSlotRecursive(child, slotName);
- if (found != null)
- {
- return found;
- }
- }
- return null;
- }
- private Transform FindEmptySlot()
- {
- foreach (Transform page in book.transform)
- {
- foreach (Transform slot in page)
- {
- if (slot.childCount == 0) // If the slot is empty
- {
- return slot;
- }
- }
- }
- return null;
- }
- private void CollectCard(GameObject card)
- {
- Card cardComponent = card.GetComponent<Card>();
- if (cardComponent == null)
- {
- Debug.LogError("Card component is missing on " + card.name);
- return;
- }
- int cardID = cardComponent.CardID;
- if (collectedCards.Contains(cardID))
- {
- Debug.Log("Card ID " + cardID + " is already in the book.");
- return;
- }
- if (book == null)
- {
- Debug.LogError("Book GameObject is not assigned.");
- return;
- }
- // Find the correct slot based on the card ID
- string slotName = cardSlotPrefix + cardID.ToString("D3");
- Transform slot = FindSlotRecursive(book.transform, slotName);
- if (slot == null)
- {
- slot = FindEmptySlot(); // Try finding a generic empty slot
- }
- if (slot != null)
- {
- // Move the card to the slot
- card.transform.SetParent(slot, false); // Keeps world position when parenting
- card.transform.localPosition = Vector3.zero; // Make sure the card is centered in the slot
- card.transform.localRotation = Quaternion.identity; // Reset rotation
- // Ensure scale is correct
- Vector3 slotSize = slot.GetComponent<Renderer>().bounds.size;
- Vector3 cardSize = card.GetComponent<Renderer>().bounds.size;
- float scaleFactor = Mathf.Min(slotSize.x / cardSize.x, slotSize.y / cardSize.y, slotSize.z / cardSize.z);
- card.transform.localScale = Vector3.one * scaleFactor;
- // Ensure card is visible
- if (card.TryGetComponent<Renderer>(out Renderer cardRenderer))
- {
- cardRenderer.enabled = true; // Ensure card is visible
- }
- else
- {
- Debug.LogWarning("Renderer component is missing on " + card.name);
- }
- // Hide card on pickup and show inside book
- if (card.TryGetComponent<Renderer>(out Renderer pickupRenderer))
- {
- pickupRenderer.enabled = false; // Hide when picked up
- StartCoroutine(ShowCard(pickupRenderer));
- }
- // Store the collected card ID
- collectedCards.Add(cardID);
- Debug.Log("Card collected and placed in slot: " + slotName);
- }
- else
- {
- Debug.Log("No available slot. Book might be full.");
- }
- }
- private IEnumerator ShowCard(Renderer cardRenderer)
- {
- yield return new WaitForSeconds(0.2f);
- cardRenderer.enabled = true;
- }
- }
Add Comment
Please, Sign In to add comment