PlayerTrigger.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerTrigger : MonoBehaviour
  5. {
  6. private Vector3 m_LastPos;
  7. //public GameObject MovePanel;
  8. private bool m_IsDown;
  9. private string m_Dir;
  10. [Range(0.1f, 10f)]
  11. public float Speed = 0.4f;
  12. public List<Vector3> MovePoints
  13. {
  14. get; set;
  15. }
  16. private int m_MoveIndex;
  17. private void Start()
  18. {
  19. m_LastPos = Vector3.zero;
  20. //MovePanel.SetActive(false);
  21. m_IsDown = false;
  22. m_MoveIndex = 0;
  23. MovePoints = new List<Vector3>();
  24. }
  25. private void LateUpdate()
  26. {
  27. /*
  28. if (API_GSXR_Slam.GSXR_Get_Head() != null)
  29. {
  30. transform.position = new Vector3(API_GSXR_Slam.GSXR_Get_Head().position.x, transform.position.y, API_GSXR_Slam.GSXR_Get_Head().position.z);
  31. transform.eulerAngles = new Vector3(0, API_GSXR_Slam.GSXR_Get_Head().eulerAngles.y, 0);
  32. if (GameManager.Instance.IsRuning)
  33. {
  34. //MovePanel.SetActive(true);
  35. //MovePanel.transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
  36. //MovePanel.transform.localPosition = transform.localPosition + transform.forward * 0.8f + new Vector3(0, -1f, 0);
  37. if (Vector3.Distance(m_LastPos, transform.position) < 0.1f)
  38. {
  39. return;
  40. }
  41. m_LastPos = transform.position;
  42. float x = transform.localPosition.x / GameManager.Instance.MapSize.x * GameManager.Instance.WebMapSize.x;
  43. float y = transform.localPosition.z / GameManager.Instance.MapSize.y * GameManager.Instance.WebMapSize.y;
  44. var pos = new Vector3(x, -y, 0);
  45. HttpSocket.Instance.SendIpToPointPos(pos, (message) =>
  46. {
  47. //Debug.Log(message);
  48. });
  49. }
  50. }
  51. */
  52. if (OpenXRCamera.Instance.head != null)
  53. {
  54. transform.position = new Vector3(OpenXRCamera.Instance.head.position.x, OpenXRCamera.Instance.head.position.y, OpenXRCamera.Instance.head.position.z);
  55. transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.eulerAngles.y, 0);
  56. if (GameManager.Instance.IsRuning)
  57. {
  58. //MovePanel.SetActive(true);
  59. //MovePanel.transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
  60. //MovePanel.transform.localPosition = transform.localPosition + transform.forward * 0.8f + new Vector3(0, -1f, 0);
  61. if (Vector3.Distance(m_LastPos, transform.position) < 0.1f)
  62. {
  63. return;
  64. }
  65. m_LastPos = transform.position;
  66. float x = transform.localPosition.x / GameManager.Instance.MapSize.x * GameManager.Instance.WebMapSize.x;
  67. float y = transform.localPosition.z / GameManager.Instance.MapSize.y * GameManager.Instance.WebMapSize.y;
  68. var pos = new Vector3(x, -y, 0);
  69. HttpSocket.Instance.SendIpToPointPos(pos, (message) =>
  70. {
  71. //Debug.Log(message);
  72. });
  73. }
  74. }
  75. }
  76. private void OnTriggerEnter(Collider other)
  77. {
  78. if (other.gameObject.tag == "TriggerBox")
  79. {
  80. GameManager.Instance.OnColliderTriggerEnter(other);
  81. Debug.Log("OnTriggerEnter " + other.name);
  82. }
  83. }
  84. private void OnTriggerExit(Collider other)
  85. {
  86. if (other.gameObject.tag == "TriggerBox")
  87. {
  88. GameManager.Instance.OnColliderTriggerExit(other);
  89. Debug.Log("OnTriggerExit " + other.name);
  90. }
  91. }
  92. #region Player Move
  93. public void PlayerMove(string dir)
  94. {
  95. m_IsDown = true;
  96. m_Dir = dir;
  97. StartCoroutine(CalMove());
  98. }
  99. public void StopMove()
  100. {
  101. m_IsDown = false;
  102. //StopCoroutine(CalMove());
  103. StopAllCoroutines();
  104. m_Dir = null;
  105. }
  106. IEnumerator CalMove()
  107. {
  108. while (m_IsDown)
  109. {
  110. //yield return new WaitForSeconds(0.05f);
  111. yield return new WaitForFixedUpdate();
  112. Vector3 pos = Vector3.zero;
  113. switch (m_Dir)
  114. {
  115. case "Front":
  116. //API_GSXR_Slam.GSXR_Get_Head().Translate(Vector3.forward * Time.deltaTime * Speed);
  117. pos = transform.forward * Time.deltaTime * Speed;
  118. break;
  119. case "After":
  120. //API_GSXR_Slam.GSXR_Get_Head().Translate(Vector3.back * Time.deltaTime * Speed);
  121. pos = -transform.forward * Time.deltaTime * Speed;
  122. break;
  123. case "Left":
  124. //API_GSXR_Slam.GSXR_Get_Head().Translate(Vector3.left * Time.deltaTime * Speed);
  125. pos = -transform.right * Time.deltaTime * Speed;
  126. break;
  127. case "Right":
  128. //API_GSXR_Slam.GSXR_Get_Head().Translate(Vector3.right * Time.deltaTime * Speed);
  129. pos = transform.right * Time.deltaTime * Speed;
  130. break;
  131. default:
  132. break;
  133. }
  134. var position = transform.localPosition + pos;
  135. position = transform.parent.TransformPoint(position);
  136. Debug.Log(position);
  137. #if UNITY_EDITOR
  138. API_GSXR_Slam.GSXR_Get_Head().position = position;
  139. #elif UNITY_ANDROID
  140. API_GSXR_Slam.GSXR_Get_Head().position = position;
  141. #endif
  142. Debug.Log("m_Dir:" + m_Dir.ToString() + "=====" + API_GSXR_Slam.GSXR_Get_Head().localPosition.ToString());
  143. }
  144. }
  145. public void MoveNextPoint()
  146. {
  147. if (MovePoints.Count > 0)
  148. {
  149. m_MoveIndex++;
  150. m_MoveIndex = m_MoveIndex < MovePoints.Count ? m_MoveIndex : 0;
  151. var position = transform.parent.TransformPoint(new Vector3(MovePoints[m_MoveIndex].x, transform.localPosition.y, MovePoints[m_MoveIndex].z));
  152. #if UNITY_EDITOR
  153. API_GSXR_Slam.GSXR_Get_Head().position = position;
  154. #elif UNITY_ANDROID
  155. API_GSXR_Slam.GSXR_Get_Head().position = position;
  156. #endif
  157. }
  158. Debug.Log("m_MoveIndex:" + m_MoveIndex.ToString() + "=====" + API_GSXR_Slam.GSXR_Get_Head().localPosition.ToString());
  159. Debug.Log(transform.position);
  160. }
  161. #endregion
  162. }