MoveCurtains.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 拼图控制
  6. /// </summary>
  7. public class MoveCurtains : 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. public bool c5r = false;
  30. public bool c5l = false;
  31. public bool duoA = false;
  32. public bool duo2A = false;
  33. public bool C3A = false;
  34. private void Start()
  35. {
  36. }
  37. void Update()
  38. {
  39. //鼠标信息
  40. //按下鼠标
  41. if (Input.GetMouseButtonDown(0))
  42. {
  43. TouchClick();
  44. }
  45. //移动鼠标
  46. if (isSelected)
  47. {
  48. MovePiece();
  49. }
  50. //释放鼠标
  51. if (Input.GetMouseButtonUp(0))
  52. {
  53. FreePiece();
  54. }
  55. }
  56. /// <summary>
  57. /// 定位点击点
  58. /// </summary>
  59. void TouchClick()
  60. {
  61. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从相机到点的触控点
  62. RaycastHit hit;
  63. if (Physics.Raycast(ray, out hit))
  64. { //接收触控,如果有击中目标
  65. //print(hit.collider.transform.parent.tag);
  66. if (hit.collider.gameObject.tag == "shui")
  67. {
  68. distanceRay = hit.distance;
  69. piece = hit.collider.gameObject;
  70. offset = hit.point - hit.collider.gameObject.transform.position;
  71. isSelected = true;
  72. }
  73. else if (hit.collider.gameObject.transform.parent &&
  74. (
  75. (hit.collider.gameObject.transform.parent.name == "group" && hit.collider.transform.parent.tag == "shui") ||
  76. (hit.collider.gameObject.transform.parent.name == "group2" && hit.collider.transform.parent.tag == "shui") ||
  77. (hit.collider.gameObject.transform.parent.name == "group3" && hit.collider.transform.parent.tag == "shui") ||
  78. (hit.collider.gameObject.transform.parent.name == "group4" && hit.collider.transform.parent.tag == "shui")
  79. )
  80. )
  81. {
  82. distanceRay = hit.distance;
  83. piece = hit.collider.gameObject.transform.parent.gameObject;
  84. offset = hit.point - hit.collider.gameObject.transform.parent.position;
  85. isSelected = true;
  86. }
  87. else
  88. {
  89. isSelected = false;
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// 移动碎片(每帧调用)
  95. /// </summary>
  96. void MovePiece()
  97. {
  98. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  99. Vector3 limitRay = ray.GetPoint(distanceRay);
  100. limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
  101. piece.transform.position = limitRay - offset;
  102. }
  103. /// <summary>
  104. /// 释放碎片
  105. /// </summary>
  106. void FreePiece()
  107. {
  108. if (isSelected)
  109. {
  110. GameObject C2 = GameObject.Find("group/C2");
  111. float C2X = C2.transform.position.x;
  112. float C2Y = C2.transform.position.y;
  113. float C2Z = C2.transform.position.z;
  114. GameObject ai = GameObject.Find("group2/ai");
  115. float aiX = ai.transform.position.x;
  116. float aiY = ai.transform.position.y;
  117. float aiZ = ai.transform.position.z;
  118. GameObject C1 = GameObject.Find("group3/C1");
  119. float C1X = C1.transform.position.x;
  120. float C1Y = C1.transform.position.y;
  121. float C1Z = C1.transform.position.z;
  122. GameObject C4 = GameObject.Find("group4/C4");
  123. float C4X = C4.transform.position.x;
  124. float C4Y = C4.transform.position.y;
  125. float C4Z = C4.transform.position.z;
  126. if (piece.name == "C5")
  127. {
  128. if (
  129. (C2X - piece.transform.position.x < 2.18f && C2X - piece.transform.position.x > 1.18f) &&
  130. (C2Y - piece.transform.position.y < -1.45f && C2Y - piece.transform.position.y > -2.45f) &&
  131. (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && !c5r
  132. )
  133. {
  134. piece.transform.SetParent(C2.transform.parent);
  135. piece.transform.position = new Vector3(C2X - 1.68f, C2Y + 1.95f, C2Z);
  136. clickSFX.Play();
  137. c5r = true;
  138. piece.tag = piece.name;
  139. }
  140. else if (
  141. (C2X - piece.transform.position.x < -1.18f && C2X - piece.transform.position.x > -2.18f) &&
  142. (C2Y - piece.transform.position.y < -1.45f && C2Y - piece.transform.position.y > -2.45f) &&
  143. (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && !c5l
  144. )
  145. {
  146. piece.transform.SetParent(C2.transform.parent);
  147. piece.transform.position = new Vector3(C2X + 1.68f, C2Y + 1.95f, C2Z);
  148. clickSFX.Play();
  149. c5l = true;
  150. piece.tag = piece.name;
  151. }
  152. }
  153. if (piece.name == "group3")
  154. {
  155. if (
  156. (C2X - piece.transform.position.x < -0.68f && C2X - piece.transform.position.x > -1.68f) &&
  157. (C2Y - piece.transform.position.y < -2.7f && C2Y - piece.transform.position.y > -3.7f) &&
  158. (C2Z - piece.transform.position.z < 0.5f && C2Z - piece.transform.position.z > -0.5f) && c5l && c5r
  159. )
  160. {
  161. piece.transform.SetParent(C2.transform.parent);
  162. piece.transform.position = new Vector3(C2X + 1.18f, C2Y + 3.2f, C2Z);
  163. clickSFX.Play();
  164. piece.tag = "TempTag";
  165. }
  166. }
  167. if (piece.name == "tou")
  168. {
  169. if (
  170. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  171. (aiY - piece.transform.position.y < -0.45f && aiY - piece.transform.position.y > -1.45f) &&
  172. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  173. )
  174. {
  175. piece.transform.SetParent(ai.transform.parent);
  176. piece.transform.position = new Vector3(aiX, aiY + 0.95f, aiZ);
  177. clickSFX.Play();
  178. piece.tag = piece.name;
  179. }
  180. }
  181. if (piece.name == "screen")
  182. {
  183. if (
  184. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  185. (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
  186. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  187. )
  188. {
  189. piece.transform.SetParent(ai.transform.parent);
  190. piece.transform.position = new Vector3(aiX, aiY, aiZ + 0.1f);
  191. clickSFX.Play();
  192. piece.tag = piece.name;
  193. }
  194. }
  195. if (piece.name == "tuozhan")
  196. {
  197. if (
  198. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  199. (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
  200. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  201. )
  202. {
  203. piece.transform.SetParent(ai.transform.parent);
  204. piece.transform.position = new Vector3(aiX, aiY, aiZ - 0.1f);
  205. clickSFX.Play();
  206. piece.tag = piece.name;
  207. }
  208. }
  209. if (piece.name == "group2")
  210. {
  211. if (
  212. (C1X - piece.transform.position.x < -1.15f && C1X - piece.transform.position.x > -2.15f) &&
  213. (C1Y - piece.transform.position.y < 0.6f && C1Y - piece.transform.position.y > -0.6f) &&
  214. (C1Z - piece.transform.position.z < 0.5f && C1Z - piece.transform.position.z > -0.5f)
  215. )
  216. {
  217. piece.transform.SetParent(C1.transform.parent);
  218. piece.transform.position = new Vector3(C1X + 1.65f, C1Y - 0.14f, C1Z + 0.06f);
  219. clickSFX.Play();
  220. piece.tag = "TempTag";
  221. }
  222. }
  223. if (piece.name == "duo1")
  224. {
  225. if (
  226. (C1X - piece.transform.position.x < -2.5f && C1X - piece.transform.position.x > -3.5f) &&
  227. (C1Y - piece.transform.position.y < 0.1f && C1Y - piece.transform.position.y > -0.9f) &&
  228. (C1Z - piece.transform.position.z < 0.5f && C1Z - piece.transform.position.z > -0.5f)
  229. )
  230. {
  231. piece.transform.SetParent(C1.transform.parent);
  232. piece.transform.position = new Vector3(C1X+3, C1Y+0.445f, C1Z);
  233. clickSFX.Play();
  234. piece.tag = piece.name;
  235. duoA = true;
  236. }
  237. }
  238. if (piece.name == "cur")
  239. {
  240. if (
  241. (C2X - piece.transform.position.x < 0.5f && C2X - piece.transform.position.x > -0.5f) &&
  242. (C2Y - piece.transform.position.y < -1.35f && C2Y - piece.transform.position.y > -2.35f) &&
  243. (C2Z - piece.transform.position.z < 0.3f && C2Z - piece.transform.position.z > -0.75f) && c5l && c5r
  244. )
  245. {
  246. piece.transform.SetParent(C2.transform.parent);
  247. piece.transform.position = new Vector3(C2X , C2Y + 1.85f, C2Z + 0.26f);
  248. clickSFX.Play();
  249. piece.tag = piece.name;
  250. }
  251. }
  252. if (piece.name == "duo2")
  253. {
  254. if (
  255. (C4X - piece.transform.position.x < -0.5f && C4X - piece.transform.position.x > -1.75f) &&
  256. (C4Y - piece.transform.position.y < 1.2f && C4Y - piece.transform.position.y > 0.2f) &&
  257. (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f)
  258. )
  259. {
  260. piece.transform.SetParent(C4.transform.parent);
  261. piece.transform.position = new Vector3(C4X + 1.24f, C4Y - 0.7f, C4Z + 0.06f);
  262. clickSFX.Play();
  263. duo2A = true;
  264. piece.tag = piece.name;
  265. }
  266. }
  267. if (piece.name == "C3")
  268. {
  269. if (
  270. (C4X - piece.transform.position.x < 3.35f && C4X - piece.transform.position.x > 2.35f) &&
  271. (C4Y - piece.transform.position.y < 0.5f && C4Y - piece.transform.position.y > -0.5f) &&
  272. (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f)
  273. )
  274. {
  275. piece.transform.SetParent(C4.transform.parent);
  276. piece.transform.position = new Vector3(C4X - 2.85f, C4Y+ 0.03f, C4Z);
  277. clickSFX.Play();
  278. C3A = true;
  279. piece.tag = piece.name;
  280. }
  281. }
  282. if (piece.name == "C6")
  283. {
  284. if (
  285. (C4X - piece.transform.position.x < 4.51f && C4X - piece.transform.position.x > 3.61f) &&
  286. (C4Y - piece.transform.position.y < 1.07f && C4Y - piece.transform.position.y > 0.17f) &&
  287. (C4Z - piece.transform.position.z < 0.5f && C4Z - piece.transform.position.z > -0.5f) && C3A
  288. )
  289. {
  290. piece.transform.SetParent(C4.transform.parent);
  291. piece.transform.position = new Vector3(C4X - 4.11f, C4Y - 0.67f, C4Z);
  292. clickSFX.Play();
  293. piece.tag = piece.name;
  294. }
  295. }
  296. if (piece.name == "group4")
  297. {
  298. if (
  299. (C1X - piece.transform.position.x < -1.1f && C1X - piece.transform.position.x > -2.1f) &&
  300. (C1Y - piece.transform.position.y < -0.9f && C1Y - piece.transform.position.y > -1.9f) &&
  301. (C1Z - piece.transform.position.z < 0.3f && C1Z - piece.transform.position.z > -0.7f) && duo2A && duoA
  302. )
  303. {
  304. piece.transform.SetParent(C1.transform.parent);
  305. piece.transform.position = new Vector3(C1X + 1.6f, C1Y + 1.4f, C1Z + 0.2f);
  306. clickSFX.Play();
  307. piece.tag = "TempTag";
  308. }
  309. }
  310. if (C2.transform.parent.childCount == 5 && acitve && ai.transform.parent.childCount == 4 && C1.transform.parent.childCount == 4 && C4.transform.parent.childCount == 4)
  311. {
  312. //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
  313. acitve = false;
  314. }
  315. else
  316. {
  317. acitve = true;
  318. }
  319. isSelected = false;
  320. }
  321. }
  322. }