MoveCar.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 拼图控制
  6. /// </summary>
  7. public class MoveCar : 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. public bool wheel1 = false;
  19. public bool wheel2 = false;
  20. Vector3 offset;
  21. GameObject lightC;
  22. public float positionX = 0f;
  23. public float positionY = 0f;
  24. public float positionZ = 0f;
  25. Vector3 positionL;
  26. //数字资源整合
  27. [Header("SFX")]
  28. public AudioSource clickSFX;
  29. public GameObject menu;
  30. public bool acitve = true;
  31. private void Start()
  32. {
  33. }
  34. void Update()
  35. {
  36. //鼠标信息
  37. //按下鼠标
  38. if (Input.GetMouseButtonDown(0))
  39. {
  40. TouchClick();
  41. }
  42. //移动鼠标
  43. if (isSelected)
  44. {
  45. MovePiece();
  46. }
  47. //释放鼠标
  48. if (Input.GetMouseButtonUp(0))
  49. {
  50. FreePiece();
  51. }
  52. }
  53. /// <summary>
  54. /// 定位点击点
  55. /// </summary>
  56. void TouchClick()
  57. {
  58. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从相机到点的触控点
  59. RaycastHit hit;
  60. if (Physics.Raycast(ray, out hit))
  61. { //接收触控,如果有击中目标
  62. //print(hit.collider.transform.parent.tag);
  63. if (hit.collider.gameObject.tag == "shui")
  64. {
  65. distanceRay = hit.distance;
  66. piece = hit.collider.gameObject;
  67. offset = hit.point - hit.collider.gameObject.transform.position;
  68. isSelected = true;
  69. }
  70. else if (hit.collider.gameObject.transform.parent &&
  71. (hit.collider.gameObject.transform.parent.name == "GrupoPiezas" ||
  72. (hit.collider.gameObject.transform.parent.name == "f1group1" && hit.collider.transform.parent.tag == "shui") ||
  73. (hit.collider.gameObject.transform.parent.name == "f1group2" && hit.collider.transform.parent.tag == "shui") ||
  74. (hit.collider.gameObject.transform.parent.name == "f2group" && hit.collider.transform.parent.tag == "shui") ||
  75. (hit.collider.gameObject.transform.parent.name == "f3group" && hit.collider.transform.parent.tag == "shui") ||
  76. (hit.collider.gameObject.transform.parent.name == "aigroup" && hit.collider.transform.parent.tag == "shui") ||
  77. (hit.collider.gameObject.transform.parent.name == "lun" && hit.collider.transform.parent.tag == "shui")
  78. ))
  79. {
  80. distanceRay = hit.distance;
  81. piece = hit.collider.gameObject.transform.parent.gameObject;
  82. offset = hit.point - hit.collider.gameObject.transform.parent.position;
  83. isSelected = true;
  84. }
  85. else
  86. {
  87. isSelected = false;
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 移动碎片(每帧调用)
  93. /// </summary>
  94. void MovePiece()
  95. {
  96. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  97. Vector3 limitRay = ray.GetPoint(distanceRay);
  98. limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
  99. piece.transform.position = limitRay - offset;
  100. }
  101. /// <summary>
  102. /// 释放碎片
  103. /// </summary>
  104. void FreePiece()
  105. {
  106. if (isSelected)
  107. {
  108. GameObject f2 = GameObject.Find("f2group/f2");
  109. float f2positionX = f2.transform.position.x;
  110. float f2positionY = f2.transform.position.y;
  111. float f2positionZ = f2.transform.position.z;
  112. GameObject f11 = GameObject.Find("f1group1/f1");
  113. float f11positionX = f11.transform.position.x;
  114. float f11positionY = f11.transform.position.y;
  115. float f11positionZ = f11.transform.position.z;
  116. GameObject f12 = GameObject.Find("f1group2/f1");
  117. float f12positionX = f12.transform.position.x;
  118. float f12positionY = f12.transform.position.y;
  119. float f12positionZ = f12.transform.position.z;
  120. if (piece.name == "f1group1")
  121. {
  122. if (
  123. (f2positionX - piece.transform.position.x < 0.5f && f2positionX - piece.transform.position.x > -0.5f) &&
  124. (f2positionY - piece.transform.position.y < -0.4f && f2positionY - piece.transform.position.y > -1.4f) &&
  125. (f2positionZ - piece.transform.position.z < -0.5f && f2positionZ - piece.transform.position.z > -1.5f)
  126. )
  127. {
  128. piece.transform.SetParent(f2.transform.parent);
  129. piece.transform.position = new Vector3(f2positionX, f2positionY + 0.9f, f2positionZ + 1f);
  130. clickSFX.Play();
  131. piece.tag = "TempTag";
  132. }
  133. }
  134. if (piece.name == "f1group2")
  135. {
  136. if (
  137. (f2positionX - piece.transform.position.x < 0.5f && f2positionX - piece.transform.position.x > -0.5f) &&
  138. (f2positionY - piece.transform.position.y < -0.4f && f2positionY - piece.transform.position.y > -1.4f) &&
  139. (f2positionZ - piece.transform.position.z < 1.5f && f2positionZ - piece.transform.position.z > 0.5f)
  140. )
  141. {
  142. piece.transform.SetParent(f2.transform.parent);
  143. piece.transform.position = new Vector3(f2positionX, f2positionY + 0.9f, f2positionZ - 1f);
  144. clickSFX.Play();
  145. piece.tag = "TempTag";
  146. }
  147. }
  148. if (piece.name == "duo1")
  149. {
  150. if (
  151. (f11positionX - piece.transform.position.x < 0.5f && f11positionX - piece.transform.position.x > -1.5f) &&
  152. (f11positionY - piece.transform.position.y < 1.3f && f11positionY - piece.transform.position.y > -0.7f) &&
  153. (f11positionZ - piece.transform.position.z < 1.3f && f11positionZ - piece.transform.position.z > -0.7f)
  154. )
  155. {
  156. piece.transform.SetParent(f11.transform.parent);
  157. piece.transform.position = new Vector3(f11positionX + 0.52f, f11positionY - 0.3f, f11positionZ - 0.3f);
  158. clickSFX.Play();
  159. piece.tag = piece.name;
  160. }
  161. }
  162. if (piece.name == "duo2")
  163. {
  164. if (
  165. (f12positionX - piece.transform.position.x < 0.5f && f12positionX - piece.transform.position.x > -1.5f) &&
  166. (f12positionY - piece.transform.position.y < 1.3f && f12positionY - piece.transform.position.y > -0.7f) &&
  167. (f12positionZ - piece.transform.position.z < 0.7f && f12positionZ - piece.transform.position.z > -1.3f)
  168. )
  169. {
  170. piece.transform.SetParent(f12.transform.parent);
  171. piece.transform.position = new Vector3(f12positionX + 0.52f, f12positionY - 0.3f, f12positionZ + 0.3f);
  172. clickSFX.Play();
  173. piece.tag = piece.name;
  174. }
  175. }
  176. if (piece.name == "wheel")
  177. {
  178. if (
  179. (f11positionX - piece.transform.position.x < -0.3f && f11positionX - piece.transform.position.x > -2.3f) &&
  180. (f11positionY - piece.transform.position.y < 1.4f && f11positionY - piece.transform.position.y > -0.6f) &&
  181. (f11positionZ - piece.transform.position.z < 0.9f && f11positionZ - piece.transform.position.z > -1.1f) && !wheel1
  182. )
  183. {
  184. piece.transform.SetParent(f11.transform.parent);
  185. wheel1 = true;
  186. piece.transform.position = new Vector3(f11positionX + 1.3f, f11positionY - 0.4f, f11positionZ + 0.1f);
  187. clickSFX.Play();
  188. piece.tag = piece.name;
  189. }
  190. }
  191. if (piece.name == "wheel")
  192. {
  193. if (
  194. (f12positionX - piece.transform.position.x < -0.3f && f12positionX - piece.transform.position.x > -2.3f) &&
  195. (f12positionY - piece.transform.position.y < 1.4f && f12positionY - piece.transform.position.y > -0.6f) &&
  196. (f12positionZ - piece.transform.position.z < 1.1f && f12positionZ - piece.transform.position.z > -0.9f) && !wheel2
  197. )
  198. {
  199. piece.transform.SetParent(f12.transform.parent);
  200. wheel2 = true;
  201. piece.transform.position = new Vector3(f12positionX + 1.3f, f12positionY - 0.4f, f12positionZ - 0.1f);
  202. clickSFX.Play();
  203. piece.tag = piece.name;
  204. }
  205. }
  206. isSelected = false;
  207. }
  208. }
  209. }