MoveNecklace.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 拼图控制
  6. /// </summary>
  7. public class MoveNecklace : MonoBehaviour
  8. {
  9. //执行此选择
  10. bool isSelected;
  11. float distanceRay; //定位点击时计算
  12. GameObject piece; //分配点击时,将移动。
  13. Transform[] piecesMove;
  14. //[HideInInspector]
  15. //public float positionZ = -0.001f;
  16. [HideInInspector]
  17. public bool puzzlePlaying;
  18. Vector3 offset;
  19. GameObject lightC;
  20. public float positionX = 0f;
  21. public float positionY = 0f;
  22. public float positionZ = 0f;
  23. Vector3 positionL;
  24. //数字资源整合
  25. [Header("SFX")]
  26. public AudioSource clickSFX;
  27. public GameObject menu;
  28. public bool acitve = true;
  29. private void Start()
  30. {
  31. }
  32. void Update()
  33. {
  34. //鼠标信息
  35. //按下鼠标
  36. if (Input.GetMouseButtonDown(0))
  37. {
  38. TouchClick();
  39. }
  40. //移动鼠标
  41. if (isSelected)
  42. {
  43. MovePiece();
  44. }
  45. //释放鼠标
  46. if (Input.GetMouseButtonUp(0))
  47. {
  48. FreePiece();
  49. }
  50. }
  51. /// <summary>
  52. /// 定位点击点
  53. /// </summary>
  54. void TouchClick()
  55. {
  56. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从相机到点的触控点
  57. RaycastHit hit;
  58. if (Physics.Raycast(ray, out hit))
  59. { //接收触控,如果有击中目标
  60. //print(hit.collider.transform.parent.tag);
  61. if (hit.collider.gameObject.tag == "shui")
  62. {
  63. distanceRay = hit.distance;
  64. piece = hit.collider.gameObject;
  65. offset = hit.point - hit.collider.gameObject.transform.position;
  66. isSelected = true;
  67. }
  68. else if (hit.collider.gameObject.transform.parent && (hit.collider.gameObject.transform.parent.name == "GrupoPiezas" || (hit.collider.gameObject.transform.parent.name == "group" && hit.collider.transform.parent.tag == "shui")))
  69. {
  70. distanceRay = hit.distance;
  71. piece = hit.collider.gameObject.transform.parent.gameObject;
  72. offset = hit.point - hit.collider.gameObject.transform.parent.position;
  73. isSelected = true;
  74. }
  75. else
  76. {
  77. isSelected = false;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 移动碎片(每帧调用)
  83. /// </summary>
  84. void MovePiece()
  85. {
  86. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  87. Vector3 limitRay = ray.GetPoint(distanceRay);
  88. limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
  89. piece.transform.position = limitRay - offset;
  90. }
  91. /// <summary>
  92. /// 释放碎片
  93. /// </summary>
  94. void FreePiece()
  95. {
  96. if (isSelected)
  97. {
  98. lightC = GameObject.Find("light");
  99. GameObject screan = GameObject.Find("group/screan");
  100. positionL = lightC.transform.position;
  101. positionX = lightC.transform.position.x;
  102. positionY = lightC.transform.position.y;
  103. positionZ = lightC.transform.position.z;
  104. float screanpositionX = screan.transform.position.x;
  105. float screanpositionY = screan.transform.position.y;
  106. float screanpositionZ = screan.transform.position.z;
  107. //piece.transform.position = new Vector3(piece.transform.position.x, piece.transform.position.y, piece.transform.position.z);
  108. //print(screan);
  109. //print(screan.tag);
  110. if (piece.name == "group")
  111. {
  112. if (
  113. (positionX - piece.transform.position.x < 0.5f && positionX - piece.transform.position.x > -0.5f) &&
  114. (positionY - piece.transform.position.y < 0.5f && positionY - piece.transform.position.y > -0.5f) &&
  115. (positionZ - piece.transform.position.z < 0.35f && positionZ - piece.transform.position.z > -0.35f)
  116. )
  117. {
  118. piece.transform.SetParent(lightC.transform.parent);
  119. piece.transform.position = new Vector3(positionX, positionY - 0.1f, positionZ + 0.08f);
  120. clickSFX.Play();
  121. piece.tag = "screan";
  122. }
  123. }
  124. if (piece.name == "ai")
  125. {
  126. if (
  127. (positionX - piece.transform.position.x < 0.5f && positionX - piece.transform.position.x > -0.5f) &&
  128. (positionY - piece.transform.position.y < 0.5f && positionY - piece.transform.position.y > -0.5f) &&
  129. (positionZ - piece.transform.position.z < 0.35f && positionZ - piece.transform.position.z > -0.65f)
  130. )
  131. {
  132. piece.transform.SetParent(lightC.transform.parent);
  133. piece.transform.position = new Vector3(positionX, positionY - 0.1f, positionZ - 0.08f);
  134. clickSFX.Play();
  135. piece.tag = piece.name;
  136. }
  137. }
  138. if (piece.name == "tou")
  139. {
  140. if (
  141. (screanpositionX - piece.transform.position.x < 0.5f && screanpositionX - piece.transform.position.x > -0.5f) &&
  142. (screanpositionY - piece.transform.position.y < 1.5f && screanpositionY - piece.transform.position.y > -1.5f) &&
  143. (screanpositionZ - piece.transform.position.z < 0.5f && screanpositionZ - piece.transform.position.z > -0.5f)
  144. )
  145. {
  146. piece.transform.SetParent(screan.transform.parent);
  147. piece.transform.position = new Vector3(screanpositionX, screanpositionY + 0.7f, screanpositionZ);
  148. clickSFX.Play();
  149. piece.tag = piece.name;
  150. }
  151. }
  152. if (piece.name == "e1" && lightC.transform.parent.childCount == 3 && screan.transform.parent.childCount == 2)
  153. {
  154. if (
  155. (positionX - piece.transform.position.x < 1.3f && positionX - piece.transform.position.x > -0.4f) &&
  156. (positionY - piece.transform.position.y < 1.3f && positionY - piece.transform.position.y > 0.3f) &&
  157. (positionZ - piece.transform.position.z < 1.5f && positionZ - piece.transform.position.z > -1.5f)
  158. )
  159. {
  160. piece.transform.SetParent(lightC.transform.parent);
  161. piece.transform.position = new Vector3(positionX-0.55f, positionY - 0.8f, positionZ);
  162. clickSFX.Play();
  163. piece.tag = piece.name;
  164. }
  165. }
  166. if (piece.name == "e2" && lightC.transform.parent.childCount == 4 && screan.transform.parent.childCount == 2)
  167. {
  168. if (
  169. (positionX - piece.transform.position.x < 2.3f && positionX - piece.transform.position.x > 1.5f) &&
  170. (positionY - piece.transform.position.y < 1.5f && positionY - piece.transform.position.y > 0.5f) &&
  171. (positionZ - piece.transform.position.z < 0.5f && positionZ - piece.transform.position.z > -0.5f)
  172. )
  173. {
  174. piece.transform.SetParent(lightC.transform.parent);
  175. piece.transform.position = new Vector3(positionX-1.92f, positionY - 1f, positionZ);
  176. clickSFX.Play();
  177. piece.tag = piece.name;
  178. }
  179. }
  180. //print(lightC.transform.parent.childCount);
  181. if (lightC.transform.parent.childCount == 5 && acitve && screan.transform.parent.childCount == 2)
  182. {
  183. //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
  184. acitve = false;
  185. }
  186. else
  187. {
  188. acitve = true;
  189. }
  190. isSelected = false;
  191. }
  192. }
  193. }