MoveDoor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 拼图控制
  6. /// </summary>
  7. public class MoveDoor : 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 b2A = false;
  30. public bool b3A = false;
  31. public bool b5A = false;
  32. private void Start()
  33. {
  34. }
  35. void Update()
  36. {
  37. //鼠标信息
  38. //按下鼠标
  39. if (Input.GetMouseButtonDown(0))
  40. {
  41. TouchClick();
  42. }
  43. //移动鼠标
  44. if (isSelected)
  45. {
  46. MovePiece();
  47. }
  48. //释放鼠标
  49. if (Input.GetMouseButtonUp(0))
  50. {
  51. FreePiece();
  52. }
  53. }
  54. /// <summary>
  55. /// 定位点击点
  56. /// </summary>
  57. void TouchClick()
  58. {
  59. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从相机到点的触控点
  60. RaycastHit hit;
  61. if (Physics.Raycast(ray, out hit))
  62. { //接收触控,如果有击中目标
  63. //print(hit.collider.transform.parent.tag);
  64. //print(hit.collider.transform.parent.name);
  65. if (hit.collider.gameObject.tag == "shui")
  66. {
  67. distanceRay = hit.distance;
  68. piece = hit.collider.gameObject;
  69. offset = hit.point - hit.collider.gameObject.transform.position;
  70. isSelected = true;
  71. }
  72. else if (
  73. hit.collider.gameObject.transform.parent &&
  74. (
  75. (hit.collider.gameObject.transform.parent.name == "group3" && 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 == "group" && hit.collider.transform.parent.tag == "shui") ||
  78. (hit.collider.gameObject.transform.parent.name == "sgroup" && hit.collider.transform.parent.tag == "shui") ||
  79. (hit.collider.gameObject.transform.parent.name == "group4" && hit.collider.transform.parent.tag == "shui")
  80. )
  81. )
  82. {
  83. distanceRay = hit.distance;
  84. piece = hit.collider.gameObject.transform.parent.gameObject;
  85. offset = hit.point - hit.collider.gameObject.transform.parent.position;
  86. isSelected = true;
  87. }
  88. else
  89. {
  90. isSelected = false;
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 移动碎片(每帧调用)
  96. /// </summary>
  97. void MovePiece()
  98. {
  99. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  100. Vector3 limitRay = ray.GetPoint(distanceRay);
  101. limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
  102. piece.transform.position = limitRay - offset;
  103. }
  104. /// <summary>
  105. /// 释放碎片
  106. /// </summary>
  107. void FreePiece()
  108. {
  109. if (isSelected)
  110. {
  111. GameObject b6 = GameObject.Find("group3/b6");
  112. float b6X = b6.transform.position.x;
  113. float b6Y = b6.transform.position.y;
  114. float b6Z = b6.transform.position.z;
  115. GameObject b7 = GameObject.Find("group2/b7");
  116. float b7X = b7.transform.position.x;
  117. float b7Y = b7.transform.position.y;
  118. float b7Z = b7.transform.position.z;
  119. GameObject door = GameObject.Find("group4/door");
  120. float doorX = door.transform.position.x;
  121. float doorY = door.transform.position.y;
  122. float doorZ = door.transform.position.z;
  123. GameObject ai = GameObject.Find("sgroup/ai");
  124. float aiX = ai.transform.position.x;
  125. float aiY = ai.transform.position.y;
  126. float aiZ = ai.transform.position.z;
  127. GameObject b4 = GameObject.Find("group/b4");
  128. float b4X = b4.transform.position.x;
  129. float b4Y = b4.transform.position.y;
  130. float b4Z = b4.transform.position.z;
  131. if (piece.name == "b2")
  132. {
  133. if (
  134. (b6X - piece.transform.position.x < 0.8f && b6X - piece.transform.position.x > -0.5f) &&
  135. (b6Y - piece.transform.position.y < 0 && b6Y - piece.transform.position.y > -3f) &&
  136. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f)
  137. )
  138. {
  139. piece.transform.SetParent(b6.transform.parent);
  140. piece.transform.position = new Vector3(b6X - 0.29f, b6Y + 2.3f, b6Z);
  141. clickSFX.Play();
  142. piece.tag = piece.name;
  143. b2A = true;
  144. }
  145. }
  146. if (piece.name == "b3")
  147. {
  148. if (
  149. (b6X - piece.transform.position.x < -2 && b6X - piece.transform.position.x > -4) &&
  150. (b6Y - piece.transform.position.y < 0 && b6Y - piece.transform.position.y > -3f) &&
  151. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f)
  152. )
  153. {
  154. piece.transform.SetParent(b6.transform.parent);
  155. piece.transform.position = new Vector3(b6X + 3.1f, b6Y + 2.3f, b6Z);
  156. clickSFX.Play();
  157. piece.tag = piece.name;
  158. b3A = true;
  159. }
  160. }
  161. if (piece.name == "b5")
  162. {
  163. if (
  164. (b6X - piece.transform.position.x < -0.7f && b6X - piece.transform.position.x > -2) &&
  165. (b6Y - piece.transform.position.y < -3 && b6Y - piece.transform.position.y > -5.1f) &&
  166. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A
  167. )
  168. {
  169. piece.transform.SetParent(b6.transform.parent);
  170. piece.transform.position = new Vector3(b6X + 1.25f, b6Y + 4.65f, b6Z + 0.13f);
  171. clickSFX.Play();
  172. piece.tag = piece.name;
  173. b5A = true;
  174. }
  175. }
  176. if (piece.name == "duo2")
  177. {
  178. if (
  179. (b6X - piece.transform.position.x < -2.25f && b6X - piece.transform.position.x > -3.3f) &&
  180. (b6Y - piece.transform.position.y < -4 && b6Y - piece.transform.position.y > -5.4f) &&
  181. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A && b5A
  182. )
  183. {
  184. piece.transform.SetParent(b6.transform.parent);
  185. piece.transform.position = new Vector3(b6X + 2.75f, b6Y + 4.9f, b6Z + 0.22f);
  186. clickSFX.Play();
  187. piece.tag = piece.name;
  188. }
  189. }
  190. if (piece.name == "duo1")
  191. {
  192. if (
  193. (b7X - piece.transform.position.x < 0.5f && b7X - piece.transform.position.x > -0.5f) &&
  194. (b7Y - piece.transform.position.y < 0.5f && b7Y - piece.transform.position.y > -0.5f) &&
  195. (b7Z - piece.transform.position.z < 0.5f && b7Z - piece.transform.position.z > -0.5f)
  196. )
  197. {
  198. piece.transform.SetParent(b7.transform.parent);
  199. piece.transform.position = new Vector3(b7X + 0.1f, b7Y + 0.14f, b7Z);
  200. clickSFX.Play();
  201. piece.tag = piece.name;
  202. }
  203. }
  204. if (piece.name == "group2")
  205. {
  206. if (
  207. (doorX - piece.transform.position.x < -0.5f && doorX - piece.transform.position.x > -1.6f) &&
  208. (doorY - piece.transform.position.y < -1.5f && doorY - piece.transform.position.y > -2.55f) &&
  209. (doorZ - piece.transform.position.z < 0.5f && doorZ - piece.transform.position.z > -0.5f) && b7.transform.parent.childCount == 2
  210. )
  211. {
  212. piece.transform.SetParent(door.transform.parent);
  213. piece.transform.position = new Vector3(doorX + 1.1f, doorY + 2.05f, doorZ);
  214. clickSFX.Play();
  215. piece.tag = "TempTag";
  216. }
  217. }
  218. if (piece.name == "group4")
  219. {
  220. print(b6X - piece.transform.position.x);
  221. print(b6Y - piece.transform.position.y);
  222. print(b6Z - piece.transform.position.z);
  223. if (
  224. (b6X - piece.transform.position.x < -0.7f && b6X - piece.transform.position.x > -2) &&
  225. (b6Y - piece.transform.position.y < -1.7f && b6Y - piece.transform.position.y > -3f) &&
  226. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A && b3A && b5A
  227. )
  228. {
  229. piece.transform.SetParent(b6.transform.parent);
  230. piece.transform.position = new Vector3(b6X + 1.37f, b6Y + 2.28f, b6Z);
  231. clickSFX.Play();
  232. piece.tag = "TempTag";
  233. }
  234. }
  235. if (piece.name == "group")
  236. {
  237. if (
  238. (b6X - piece.transform.position.x < 2f && b6X - piece.transform.position.x > 1f) &&
  239. (b6Y - piece.transform.position.y < -1.2f && b6Y - piece.transform.position.y > -2.25f) &&
  240. (b6Z - piece.transform.position.z < 0.5f && b6Z - piece.transform.position.z > -0.5f) && b2A
  241. )
  242. {
  243. piece.transform.SetParent(b6.transform.parent);
  244. piece.transform.position = new Vector3(b6X - 1.5f, b6Y + 1.75f, b6Z);
  245. clickSFX.Play();
  246. piece.tag = "TempTag";
  247. b5A = true;
  248. }
  249. }
  250. if (piece.name == "tou")
  251. {
  252. if (
  253. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  254. (aiY - piece.transform.position.y < -0.45f && aiY - piece.transform.position.y > -1.45f) &&
  255. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  256. )
  257. {
  258. piece.transform.SetParent(ai.transform.parent);
  259. piece.transform.position = new Vector3(aiX, aiY + 0.95f, aiZ);
  260. clickSFX.Play();
  261. piece.tag = piece.name;
  262. }
  263. }
  264. if (piece.name == "screen")
  265. {
  266. if (
  267. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  268. (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
  269. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  270. )
  271. {
  272. piece.transform.SetParent(ai.transform.parent);
  273. piece.transform.position = new Vector3(aiX, aiY, aiZ + 0.1f);
  274. clickSFX.Play();
  275. piece.tag = piece.name;
  276. }
  277. }
  278. if (piece.name == "tuozhan")
  279. {
  280. if (
  281. (aiX - piece.transform.position.x < 0.5f && aiX - piece.transform.position.x > -0.5f) &&
  282. (aiY - piece.transform.position.y < 0.5f && aiY - piece.transform.position.y > -0.5f) &&
  283. (aiZ - piece.transform.position.z < 0.5f && aiZ - piece.transform.position.z > -0.5f)
  284. )
  285. {
  286. piece.transform.SetParent(ai.transform.parent);
  287. piece.transform.position = new Vector3(aiX, aiY, aiZ - 0.1f);
  288. clickSFX.Play();
  289. piece.tag = piece.name;
  290. }
  291. }
  292. if (piece.name == "sgroup")
  293. {
  294. if (
  295. (b4X - piece.transform.position.x < 0.5f && b4X - piece.transform.position.x > -0.5f) &&
  296. (b4Y - piece.transform.position.y < 0.5f && b4Y - piece.transform.position.y > -1.3f) &&
  297. (b4Z - piece.transform.position.z < 0.5f && b4Z - piece.transform.position.z > -0.5f)
  298. )
  299. {
  300. piece.transform.SetParent(b4.transform.parent);
  301. piece.transform.position = new Vector3(b4X - 0.07f, b4Y + 0.7f, b4Z + 0.05f);
  302. clickSFX.Play();
  303. piece.tag = "TempTag";
  304. }
  305. }
  306. if (b6.transform.parent.childCount == 7 && acitve && b7.transform.parent.childCount == 2 && door.transform.parent.childCount == 2 && ai.transform.parent.childCount == 4 && b4.transform.parent.childCount == 2)
  307. {
  308. //Instantiate(menu, GameObject.FindGameObjectWithTag("UI").transform);
  309. acitve = false;
  310. }
  311. else
  312. {
  313. acitve = true;
  314. }
  315. isSelected = false;
  316. }
  317. }
  318. }