PlayerTrigger.cs 5.1 KB

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