TeshMap.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 计算人物在地图上的位置
  7. /// </summary>
  8. public class TeshMap : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 定位场景模型的2个点
  12. /// </summary>
  13. public Transform mapPointA;
  14. public Transform mapPointB;
  15. // public Transform mapPointC;
  16. /// <summary>
  17. /// 角色
  18. /// </summary>
  19. public Transform player;
  20. // public Transform sceneRoot;
  21. /// <summary>
  22. /// 圆形地图
  23. /// </summary>
  24. public TestMapShow testMinMapShow;
  25. /// <summary>
  26. /// 长方形地图
  27. /// </summary>
  28. public TestMapShow testMaxMapShow;
  29. public UdpClient udpClient;
  30. public Vector2 mapSize;
  31. Vector3 oldPos;
  32. /// <summary>
  33. /// 场景尺寸 比例
  34. /// </summary>
  35. public float disZ;
  36. public float disX;
  37. float times;
  38. /// <summary>
  39. /// 场景所有水晶点
  40. /// </summary>
  41. public List<Transform> listPoint;
  42. List<Vector3> listPointPos;
  43. private void Start()
  44. {
  45. Debug.Log(mapPointA.eulerAngles + " mapPointA " + mapPointA.localEulerAngles);
  46. disX = Mathf.Abs(mapPointB.localPosition.x - mapPointA.localPosition.x);
  47. disZ = Mathf.Abs(mapPointB.localPosition.z - mapPointA.localPosition.z);
  48. MapPoint();
  49. }
  50. private void LateUpdate()
  51. {
  52. if (oldPos != player.localPosition)
  53. {
  54. oldPos = player.localPosition;
  55. MapPosRot();
  56. }
  57. times += Time.deltaTime;
  58. if (times > 0.5f && API_SVR.IsSvrInitialized())
  59. {
  60. times = 0;
  61. Vector2 mapPos = MapPos();
  62. Vector3 pos = new Vector3(mapSize.x * mapPos.x, mapSize.y * mapPos.y, 0.0f);
  63. string Ts = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
  64. udpClient.SendIpToPointCloud(pos, Ts);
  65. }
  66. }
  67. /// <summary>
  68. /// 计算位置 ( 坐标和旋转)
  69. /// </summary>
  70. private void MapPosRot()
  71. {
  72. float x = Mathf.Abs(oldPos.x - mapPointA.localPosition.x)/disX;
  73. float z = (Mathf.Abs(oldPos.z - mapPointA.localPosition.z))/disZ;
  74. //Debug.Log("MapPosX: " + disX + " " + x);
  75. //Debug.Log("MapPosZ: " + disZ + " " + z);
  76. // testMapShow.ShowPlayer(x, z,mapPointA.eulerAngles.y - mapPointA.localEulerAngles.y);
  77. SettingMapPosRot(z, x);
  78. // return pos;
  79. }
  80. private Vector2 MapPos()
  81. {
  82. float x = Mathf.Abs(oldPos.x - mapPointA.localPosition.x) / disX;
  83. float z = (Mathf.Abs(oldPos.z - mapPointA.localPosition.z)) / disZ;
  84. Vector2 pos = new Vector2(z, x);
  85. return pos;
  86. }
  87. private void SettingMapPosRot( float z, float x)
  88. {
  89. testMaxMapShow.ShowPlayer(z, x, player.transform.localEulerAngles.y + 179);
  90. testMinMapShow.MapRot(z, x, player.transform.localEulerAngles.y - 80);
  91. }
  92. /// <summary>
  93. /// 计算场景水晶点位置
  94. /// </summary>
  95. private void MapPoint()
  96. {
  97. if (listPoint.Count <= 0)
  98. return;
  99. listPointPos = new List<Vector3>();
  100. for (int i = 0; i < listPoint.Count; i++)
  101. {
  102. float x = Mathf.Abs(listPoint[i].localPosition.x - mapPointA.localPosition.x) / disX;
  103. float z = Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) / disZ;
  104. Debug.Log(Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) + " "+ disX);
  105. listPointPos.Add(new Vector3(z, x, 0));
  106. }
  107. testMaxMapShow.ShowPoint(listPointPos);
  108. testMinMapShow.LucencyMap();
  109. testMinMapShow.ShowPoint(listPointPos);
  110. testMinMapShow.LucencyMap();
  111. }
  112. }