MoveComponet.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 拼图控制
  6. /// </summary>
  7. public class MoveComponet : 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. print(hit.collider.gameObject.tag);
  62. print(hit.collider.gameObject);
  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 && (hit.collider.gameObject.transform.parent.name == "GrupoPiezas" || (hit.collider.gameObject.transform.parent.name == "group" && hit.collider.transform.parent.tag == "shui")))
  71. {
  72. distanceRay = hit.distance;
  73. piece = hit.collider.gameObject.transform.parent.gameObject;
  74. offset = hit.point - hit.collider.gameObject.transform.parent.position;
  75. isSelected = true;
  76. }
  77. else
  78. {
  79. isSelected = false;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// 移动碎片(每帧调用)
  85. /// </summary>
  86. void MovePiece()
  87. {
  88. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  89. Vector3 limitRay = ray.GetPoint(distanceRay);
  90. limitRay = new Vector3(limitRay.x, limitRay.y, limitRay.z);
  91. piece.transform.position = limitRay - offset;
  92. }
  93. /// <summary>
  94. /// 释放碎片
  95. /// </summary>
  96. void FreePiece()
  97. {
  98. if (isSelected)
  99. {
  100. //piece.transform.position = new Vector3(piece.transform.position.x, piece.transform.position.y, piece.transform.position.z);
  101. //print(screan);
  102. print(piece.transform.position);
  103. isSelected = false;
  104. }
  105. }
  106. }