Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- public class BookControls : MonoBehaviour
- {
- public static BookControls Instance;
- [SerializeField] List<Transform> pages;
- [SerializeField] float pageSpeed = 0.5f;
- public GameObject bookObject;
- public Transform playerTransform;
- public float spawnDistance = 2.0f;
- public Transform bookTransform;
- public GameObject SlotPrefab;
- public Transform[] Pages; // Array of page transforms (set in the Inspector)
- int currentPageIndex = 0;
- bool isTurningPage = false;
- private bool isBookVisible = false;
- private void Start()
- {
- InitialState();
- AdjustSlotSize(); // Call here to adjust the slot size after initializing the book
- }
- public void InitialState()
- {
- for (int i = 0; i < pages.Count; i++)
- {
- pages[i].localRotation = Quaternion.Euler(-75, 0, 0);
- }
- pages[0].SetAsLastSibling();
- }
- public void AdjustSlotSize()
- {
- foreach (Transform page in pages)
- {
- RectTransform pageRect = page.GetComponent<RectTransform>();
- // Calculate the desired slot size
- float slotWidth = pageRect.rect.width / 3; // Assuming a 3x3 grid
- float slotHeight = pageRect.rect.height / 3;
- // Find GridLayoutGroup and adjust cell size
- GridLayoutGroup gridLayout = page.GetComponent<GridLayoutGroup>();
- if (gridLayout != null)
- {
- gridLayout.cellSize = new Vector2(slotWidth, slotHeight);
- }
- }
- }
- public void RotateForward()
- {
- if (isTurningPage || currentPageIndex >= pages.Count) { return; } // Check if already turning or at the last page
- float angle = 75; // Rotation angle for turning forward
- StartCoroutine(RotatePage(pages[currentPageIndex], angle, true)); // Start coroutine to rotate page
- }
- public void RotateBack()
- {
- if (isTurningPage || currentPageIndex <= 0) { return; } // Check if already turning or at the first page
- float angle = -75; // Rotation angle for turning back
- StartCoroutine(RotatePage(pages[currentPageIndex - 1], angle, false)); // Start coroutine to rotate previous page
- }
- IEnumerator RotatePage(Transform page, float targetAngle, bool forward)
- {
- float value = 0f;
- isTurningPage = true;
- Quaternion targetRotation = Quaternion.Euler(targetAngle, 0, 0); // Target rotation for the page
- while (true)
- {
- value += Time.deltaTime * pageSpeed;
- page.localRotation = Quaternion.Slerp(page.localRotation, targetRotation, value); // Smoothly rotate the page
- float angleDifference = Quaternion.Angle(page.localRotation, targetRotation); // Calculate angle difference
- if (angleDifference < 0.1f)
- {
- page.localRotation = targetRotation; // Snap to the exact target rotation
- if (forward)
- {
- currentPageIndex++;
- }
- else
- {
- currentPageIndex--;
- }
- isTurningPage = false;
- break;
- }
- yield return null;
- }
- }
- void Update()
- {
- // Check if the "Q" key is pressed
- if (Input.GetKeyDown(KeyCode.Q))
- {
- ToggleBookVisibility();
- }
- // If the book is visible, update its position to follow the player
- if (isBookVisible)
- {
- Vector3 followPosition = playerTransform.position + playerTransform.forward * spawnDistance;
- bookObject.transform.position = followPosition;
- // Check for page turning input
- if (!isTurningPage)
- {
- if (Input.GetKeyDown(KeyCode.C))
- {
- RotateForward();
- }
- else if (Input.GetKeyDown(KeyCode.Z))
- {
- RotateBack();
- }
- }
- }
- }
- void ToggleBookVisibility()
- {
- isBookVisible = !isBookVisible;
- bookObject.SetActive(isBookVisible);
- Collider bookCollider = bookObject.GetComponent<Collider>();
- if (bookCollider != null)
- {
- bookCollider.enabled = !isBookVisible; // Disable when opened
- }
- if (isBookVisible)
- {
- bookObject.transform.position = playerTransform.position + playerTransform.forward * spawnDistance;
- bookObject.transform.parent = playerTransform;
- bookObject.transform.rotation = Quaternion.LookRotation(playerTransform.forward);
- bookTransform.Rotate(Vector3.up, -90f, Space.Self);
- bookTransform.Rotate(Vector3.forward, 15f, Space.Self);
- }
- else
- {
- bookObject.transform.parent = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement