PlayerTrigger.cs 6.7 KB

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