Raycaster.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JTSystems;
  5. using UnityEngine.EventSystems;
  6. public class Raycaster : MonoBehaviour
  7. {
  8. [Header(" Settings ")]
  9. [SerializeField] private bool playTesting;
  10. Camera mainCamera;
  11. private void Awake()
  12. {
  13. mainCamera = Camera.main;
  14. }
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. if (Input.GetMouseButtonDown(0))
  23. {
  24. if(playTesting || UIManager.IsGame())
  25. DetectClosestFolding();
  26. }
  27. }
  28. private void DetectClosestFolding()
  29. {
  30. Ray tapRay = mainCamera.ScreenPointToRay(Input.mousePosition);
  31. RaycastHit hit;
  32. Physics.Raycast(tapRay, out hit, 50);
  33. if (hit.collider != null)
  34. {
  35. Ray ray = new Ray(hit.point, (Vector3.zero - hit.point));
  36. float maxDistance = Vector3.Distance(hit.point, Vector3.zero);
  37. Paper currentPaper = FindObjectOfType<Paper>(false);
  38. Folding[] detectedFoldings = currentPaper.GetComponentsInChildren<Folding>(true);
  39. int closestFoldingIndex = -1;
  40. float minDistance = 5000;
  41. for (int i = 0; i < detectedFoldings.Length; i++)
  42. {
  43. Folding currentFolding = detectedFoldings[i];
  44. Plane plane = currentFolding.GetFoldingPlane();
  45. float enter;
  46. bool foldingIntersected = plane.Raycast(ray, out enter);
  47. if (enter >= maxDistance || !foldingIntersected)
  48. continue;
  49. if(enter < minDistance)
  50. {
  51. minDistance = enter;
  52. closestFoldingIndex = i;
  53. }
  54. }
  55. if (closestFoldingIndex >= 0)
  56. {
  57. if (!detectedFoldings[closestFoldingIndex].gameObject.activeInHierarchy)
  58. return;
  59. currentPaper = detectedFoldings[closestFoldingIndex].GetComponentInParent<Paper>();
  60. currentPaper.TryFold(detectedFoldings[closestFoldingIndex]);
  61. }
  62. /*
  63. if (enter >= maxDistance)
  64. foldingIntersected = false;
  65. if (foldingIntersected)
  66. {
  67. Paper currentPaper = currentFolding.GetComponentInParent<Paper>();
  68. currentPaper.TryFold(currentFolding);
  69. }*/
  70. //Debug.Log("Folding intersected : " + foldingIntersected + " -> enter = " + enter);
  71. }
  72. }
  73. /*
  74. private void DetectClosestFolding()
  75. {
  76. Ray tapRay = mainCamera.ScreenPointToRay(Input.mousePosition);
  77. RaycastHit hit;
  78. Physics.Raycast(tapRay, out hit, 50);
  79. if (hit.collider != null)
  80. {
  81. Folding currentFolding = FindObjectOfType<Folding>();
  82. Plane plane = currentFolding.GetFoldingPlane();
  83. Ray ray = new Ray(hit.point, (Vector3.zero - hit.point));
  84. float maxDistance = Vector3.Distance(hit.point, Vector3.zero);
  85. float enter;
  86. bool foldingIntersected = plane.Raycast(ray, out enter);
  87. if (enter >= maxDistance)
  88. foldingIntersected = false;
  89. if(foldingIntersected)
  90. {
  91. Paper currentPaper = currentFolding.GetComponentInParent<Paper>();
  92. currentPaper.TryFold(currentFolding);
  93. }
  94. Debug.Log("Folding intersected : " + foldingIntersected + " -> enter = " + enter);
  95. }
  96. }
  97. */
  98. }