Advertisement
Micro__6

Untitled

May 25th, 2025
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5.  
  6.  
  7. public class BookControls : MonoBehaviour
  8. {
  9. public static BookControls Instance;
  10. [SerializeField] List<Transform> pages;
  11. [SerializeField] float pageSpeed = 0.5f;
  12. public GameObject bookObject;
  13. public Transform playerTransform;
  14. public float spawnDistance = 2.0f;
  15. public Transform bookTransform;
  16. public GameObject SlotPrefab;
  17. public Transform[] Pages; // Array of page transforms (set in the Inspector)
  18.  
  19. int currentPageIndex = 0;
  20. bool isTurningPage = false;
  21. private bool isBookVisible = false;
  22.  
  23. private void Start()
  24. {
  25. InitialState();
  26. AdjustSlotSize(); // Call here to adjust the slot size after initializing the book
  27. }
  28.  
  29. public void InitialState()
  30. {
  31. for (int i = 0; i < pages.Count; i++)
  32. {
  33. pages[i].localRotation = Quaternion.Euler(-75, 0, 0);
  34. }
  35. pages[0].SetAsLastSibling();
  36. }
  37.  
  38. public void AdjustSlotSize()
  39. {
  40. foreach (Transform page in pages)
  41. {
  42. RectTransform pageRect = page.GetComponent<RectTransform>();
  43.  
  44. // Calculate the desired slot size
  45. float slotWidth = pageRect.rect.width / 3; // Assuming a 3x3 grid
  46. float slotHeight = pageRect.rect.height / 3;
  47.  
  48. // Find GridLayoutGroup and adjust cell size
  49. GridLayoutGroup gridLayout = page.GetComponent<GridLayoutGroup>();
  50. if (gridLayout != null)
  51. {
  52. gridLayout.cellSize = new Vector2(slotWidth, slotHeight);
  53. }
  54. }
  55. }
  56.  
  57. public void RotateForward()
  58. {
  59. if (isTurningPage || currentPageIndex >= pages.Count) { return; } // Check if already turning or at the last page
  60.  
  61. float angle = 75; // Rotation angle for turning forward
  62. StartCoroutine(RotatePage(pages[currentPageIndex], angle, true)); // Start coroutine to rotate page
  63. }
  64.  
  65. public void RotateBack()
  66. {
  67. if (isTurningPage || currentPageIndex <= 0) { return; } // Check if already turning or at the first page
  68.  
  69. float angle = -75; // Rotation angle for turning back
  70. StartCoroutine(RotatePage(pages[currentPageIndex - 1], angle, false)); // Start coroutine to rotate previous page
  71. }
  72.  
  73. IEnumerator RotatePage(Transform page, float targetAngle, bool forward)
  74. {
  75. float value = 0f;
  76. isTurningPage = true;
  77. Quaternion targetRotation = Quaternion.Euler(targetAngle, 0, 0); // Target rotation for the page
  78.  
  79. while (true)
  80. {
  81. value += Time.deltaTime * pageSpeed;
  82. page.localRotation = Quaternion.Slerp(page.localRotation, targetRotation, value); // Smoothly rotate the page
  83.  
  84. float angleDifference = Quaternion.Angle(page.localRotation, targetRotation); // Calculate angle difference
  85. if (angleDifference < 0.1f)
  86. {
  87. page.localRotation = targetRotation; // Snap to the exact target rotation
  88.  
  89. if (forward)
  90. {
  91. currentPageIndex++;
  92. }
  93. else
  94. {
  95. currentPageIndex--;
  96. }
  97. isTurningPage = false;
  98. break;
  99. }
  100. yield return null;
  101. }
  102. }
  103.  
  104. void Update()
  105. {
  106. // Check if the "Q" key is pressed
  107. if (Input.GetKeyDown(KeyCode.Q))
  108. {
  109. ToggleBookVisibility();
  110. }
  111.  
  112. // If the book is visible, update its position to follow the player
  113. if (isBookVisible)
  114. {
  115. Vector3 followPosition = playerTransform.position + playerTransform.forward * spawnDistance;
  116. bookObject.transform.position = followPosition;
  117.  
  118. // Check for page turning input
  119. if (!isTurningPage)
  120. {
  121. if (Input.GetKeyDown(KeyCode.C))
  122. {
  123. RotateForward();
  124. }
  125. else if (Input.GetKeyDown(KeyCode.Z))
  126. {
  127. RotateBack();
  128. }
  129. }
  130. }
  131. }
  132.  
  133. void ToggleBookVisibility()
  134. {
  135. isBookVisible = !isBookVisible;
  136. bookObject.SetActive(isBookVisible);
  137.  
  138. Collider bookCollider = bookObject.GetComponent<Collider>();
  139. if (bookCollider != null)
  140. {
  141. bookCollider.enabled = !isBookVisible; // Disable when opened
  142. }
  143.  
  144. if (isBookVisible)
  145. {
  146. bookObject.transform.position = playerTransform.position + playerTransform.forward * spawnDistance;
  147. bookObject.transform.parent = playerTransform;
  148. bookObject.transform.rotation = Quaternion.LookRotation(playerTransform.forward);
  149. bookTransform.Rotate(Vector3.up, -90f, Space.Self);
  150. bookTransform.Rotate(Vector3.forward, 15f, Space.Self);
  151. }
  152. else
  153. {
  154. bookObject.transform.parent = null;
  155. }
  156. }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement