|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
using Newtonsoft.Json; |
|
|
|
|
using System.Collections; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using UnityEngine; |
|
|
|
@ -8,6 +9,7 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
public Transform PathParent; |
|
|
|
|
public List<Transform> PathList = new List<Transform>(); |
|
|
|
|
public int NowIndex = 0; |
|
|
|
|
public int NowFloor = 4; |
|
|
|
|
// Start is called before the first frame update |
|
|
|
|
void Start() |
|
|
|
|
{ |
|
|
|
@ -21,8 +23,43 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
// Update is called once per frame |
|
|
|
|
void Update() |
|
|
|
|
{ |
|
|
|
|
if (Input.GetKeyDown(KeyCode.S)) |
|
|
|
|
{ |
|
|
|
|
List<LTData> alld = new List<LTData>(); |
|
|
|
|
LTData data = new LTData(); |
|
|
|
|
Vector3 p1 = new Vector3(-17.47f, -17.5f, 41.83f);//1.7 |
|
|
|
|
Vector3 p2 = new Vector3(-17.47f, -15.9f, 37.48f);//1.4 |
|
|
|
|
Vector3 p3 = new Vector3(-15.48f, -15.9f, 37.48f);//1.4 |
|
|
|
|
Vector3 p4 = new Vector3(-15.48f, -14.3f, 41.83f);//1.7 |
|
|
|
|
data.FloorId = 4; |
|
|
|
|
data.LTName = "LT65"; |
|
|
|
|
data.LTTopCode = "1"; |
|
|
|
|
data.LTMidCode = "2"; |
|
|
|
|
data.LTBottomCode = "3"; |
|
|
|
|
data.PTop = p1; |
|
|
|
|
data.PTop_Half = p2; |
|
|
|
|
data.PHalf_Bottom = p3; |
|
|
|
|
data.PBottom = p4; |
|
|
|
|
data.PWidthRange = 0.8f; |
|
|
|
|
data.PHalfWidthRange = 0.4f; |
|
|
|
|
data.IsTop_Bottom = false; |
|
|
|
|
alld.Add(data); |
|
|
|
|
alld.Add(data); |
|
|
|
|
|
|
|
|
|
string adstr = JsonConvert.SerializeObject(alld); |
|
|
|
|
Debug.Log(adstr); |
|
|
|
|
} |
|
|
|
|
if (Input.GetKeyDown(KeyCode.T)) |
|
|
|
|
{ |
|
|
|
|
LTData ltdata = null; |
|
|
|
|
foreach (var item in Config.LTPostions) |
|
|
|
|
{ |
|
|
|
|
if (item.FloorId == NowFloor) |
|
|
|
|
{ |
|
|
|
|
ltdata = item; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (NowIndex == 0) |
|
|
|
|
{ |
|
|
|
|
LocationData data = new LocationData(); |
|
|
|
@ -35,7 +72,7 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
agent.enabled = false; |
|
|
|
|
agent.GetComponent<NavMeshAgent>().enabled = false; |
|
|
|
|
GetComponent<LocationSimulator>().agentControllers.Add(data.targetNo, agent); |
|
|
|
|
Vector3 pos = ConversionTransform(data); |
|
|
|
|
Vector3 pos = ConversionTransform(data, ltdata); |
|
|
|
|
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos; |
|
|
|
|
} |
|
|
|
|
else if (NowIndex < PathList.Count) |
|
|
|
@ -46,7 +83,7 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
data.xAxis = PathList[NowIndex].transform.position.x; |
|
|
|
|
data.yAxis = PathList[NowIndex].transform.position.y; |
|
|
|
|
data.zAxis = PathList[NowIndex].transform.position.z; |
|
|
|
|
Vector3 pos = ConversionTransform(data); |
|
|
|
|
Vector3 pos = ConversionTransform(data, ltdata); |
|
|
|
|
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
@ -61,78 +98,90 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="data"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public Vector3 ConversionTransform(LocationData data) |
|
|
|
|
public Vector3 ConversionTransform(LocationData data, LTData ltData) |
|
|
|
|
{ |
|
|
|
|
//判断数据被检测到的区域 |
|
|
|
|
string St1 = Config.LTTopCode;//楼梯入口基站 |
|
|
|
|
string St2 = Config.LTMidCode;//楼梯间基站 |
|
|
|
|
string St3 = Config.LTBottomCode;//楼梯底层基站 |
|
|
|
|
//纠正坐标 |
|
|
|
|
Vector3 p1 = new Vector3(-15.48f, -17.7f, 41.83f);//1.7 |
|
|
|
|
Vector3 p2 = new Vector3(-15.48f, -19.3f, 37.48f);//1.4 |
|
|
|
|
Vector3 p3 = new Vector3(-17.47f, -19.3f, 37.48f);//1.4 |
|
|
|
|
Vector3 p4 = new Vector3(-17.47f, -21.1f, 42.42f);//1.7 |
|
|
|
|
//换算最终位置 |
|
|
|
|
//-4地面高度-17.7f |
|
|
|
|
float y = -17.7f; |
|
|
|
|
if (ltData == null) |
|
|
|
|
return Vector3.zero; |
|
|
|
|
//取最高处作为pos.y方便向下打射线 |
|
|
|
|
float y = ltData.PTop.y > ltData.PBottom.y ? ltData.PTop.y : ltData.PBottom.y; |
|
|
|
|
//对方传入转换后的数据 |
|
|
|
|
var pos = new Vector3((float)data.xAxis, y, (float)data.zAxis); |
|
|
|
|
if (data.stations.Contains(St3) && !data.stations.Contains(St2)) |
|
|
|
|
{ //-5地面高度-21.1f |
|
|
|
|
pos.y = p4.y; |
|
|
|
|
|
|
|
|
|
if (data.stations.Contains(ltData.LTBottomCode) && !data.stations.Contains(ltData.LTMidCode)) |
|
|
|
|
{ //下面地面 |
|
|
|
|
pos.y = ltData.PBottom.y; |
|
|
|
|
} |
|
|
|
|
else if (data.stations.Contains(St1) && data.stations.Contains(St2)) |
|
|
|
|
{ //-4到-5楼梯上半段 |
|
|
|
|
if (pos.x < p1.x - 0.8 || pos.x > p1.x + 0.8) |
|
|
|
|
else if (data.stations.Contains(ltData.LTBottomCode) && data.stations.Contains(ltData.LTMidCode)) |
|
|
|
|
{ //楼梯下半段 |
|
|
|
|
if (pos.x < ltData.PBottom.x - ltData.PWidthRange || pos.x > ltData.PBottom.x + ltData.PWidthRange) |
|
|
|
|
{ |
|
|
|
|
pos.x = p1.x; |
|
|
|
|
pos.x = ltData.PBottom.x; |
|
|
|
|
} |
|
|
|
|
if (pos.z > p1.z) |
|
|
|
|
if (pos.z > ltData.PBottom.z) |
|
|
|
|
{ |
|
|
|
|
pos.z = p1.z; |
|
|
|
|
pos.z = ltData.PBottom.z; |
|
|
|
|
} |
|
|
|
|
if (pos.z < p2.z) |
|
|
|
|
if (pos.z < ltData.PHalf_Bottom.z) |
|
|
|
|
{ |
|
|
|
|
pos.z = p2.z; |
|
|
|
|
pos.z = ltData.PHalf_Bottom.z; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pos.y = SetY(pos); |
|
|
|
|
pos.y = SetY(pos, ltData.LTName); |
|
|
|
|
|
|
|
|
|
if (pos.y == 0) |
|
|
|
|
pos.y = ltData.PBottom.y; |
|
|
|
|
} |
|
|
|
|
else if (data.stations.Contains(St2) && data.stations.Contains(St3)) |
|
|
|
|
{ //-4到-5楼梯下半段 |
|
|
|
|
if (pos.x < p3.x - 0.8 || pos.x > p3.x + 0.8) |
|
|
|
|
else if (data.stations.Contains(ltData.LTTopCode) && data.stations.Contains(ltData.LTMidCode)) |
|
|
|
|
{ //楼梯上半段 |
|
|
|
|
if (pos.x < ltData.PTop.x - ltData.PWidthRange || pos.x > ltData.PTop.x + ltData.PWidthRange) |
|
|
|
|
{ |
|
|
|
|
pos.x = p3.x; |
|
|
|
|
pos.x = ltData.PTop.x; |
|
|
|
|
} |
|
|
|
|
if (pos.z > p4.z) |
|
|
|
|
if (pos.z > ltData.PTop.z) |
|
|
|
|
{ |
|
|
|
|
pos.z = p4.z; |
|
|
|
|
pos.z = ltData.PTop.z; |
|
|
|
|
} |
|
|
|
|
if (pos.z < p3.z) |
|
|
|
|
if (pos.z < ltData.PTop_Half.z) |
|
|
|
|
{ |
|
|
|
|
pos.z = p3.z; |
|
|
|
|
pos.z = ltData.PTop_Half.z; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pos.y = SetY(pos); |
|
|
|
|
pos.y = SetY(pos, ltData.LTName); |
|
|
|
|
|
|
|
|
|
if (pos.y == 0) |
|
|
|
|
pos.y = ltData.PTop.y; |
|
|
|
|
} |
|
|
|
|
else if (data.stations.Contains(St2) && !data.stations.Contains(St1) && !data.stations.Contains(St3)) |
|
|
|
|
else if (data.stations.Contains(ltData.LTMidCode) && |
|
|
|
|
!data.stations.Contains(ltData.LTTopCode) && |
|
|
|
|
!data.stations.Contains(ltData.LTBottomCode)) |
|
|
|
|
{ |
|
|
|
|
//-4到-5楼梯间过度 |
|
|
|
|
if (pos.z < p3.z - 0.4 || pos.z > p3.z + 0.4) |
|
|
|
|
//楼梯间过度 |
|
|
|
|
if (pos.z < ltData.PTop_Half.z - ltData.PHalfWidthRange || pos.z > ltData.PTop_Half.z + ltData.PHalfWidthRange) |
|
|
|
|
{ |
|
|
|
|
pos.z = p3.z; |
|
|
|
|
pos.z = ltData.PTop_Half.z; |
|
|
|
|
} |
|
|
|
|
if (pos.x > p2.x) |
|
|
|
|
if (pos.x > ltData.PTop_Half.x) |
|
|
|
|
{ |
|
|
|
|
pos.x = p2.x; |
|
|
|
|
pos.x = ltData.PTop_Half.x; |
|
|
|
|
} |
|
|
|
|
if (pos.x < p3.x) |
|
|
|
|
if (pos.x < ltData.PHalf_Bottom.x) |
|
|
|
|
{ |
|
|
|
|
pos.x = p3.x; |
|
|
|
|
pos.x = ltData.PHalf_Bottom.x; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pos.y = p3.y; |
|
|
|
|
pos.y = ltData.PTop_Half.y; |
|
|
|
|
} |
|
|
|
|
else if (data.stations.Contains(ltData.LTTopCode) && !data.stations.Contains(ltData.LTMidCode)) |
|
|
|
|
{//上面地面 |
|
|
|
|
pos.y = ltData.PTop.y; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{//其他基站则在正常高度 |
|
|
|
|
if (ltData.IsTop_Bottom) |
|
|
|
|
pos.y = ltData.PTop.y; |
|
|
|
|
else |
|
|
|
|
pos.y = ltData.PBottom.y; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return pos; |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
@ -140,25 +189,17 @@ public class PathFinding : MonoBehaviour
|
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="position"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public float SetY(Vector3 position) |
|
|
|
|
public float SetY(Vector3 position, string ltname) |
|
|
|
|
{ |
|
|
|
|
RaycastHit hit; |
|
|
|
|
|
|
|
|
|
var hitlist = Physics.RaycastAll(position, Vector3.down, 100f, ~(1 << (LayerMask.NameToLayer("Defaut") | LayerMask.NameToLayer("Hidden")))); |
|
|
|
|
if (hitlist.Length > 0) |
|
|
|
|
{ |
|
|
|
|
foreach (var item in hitlist) |
|
|
|
|
{ |
|
|
|
|
if (item.transform.gameObject.layer == LayerMask.NameToLayer("Stair")) |
|
|
|
|
if (item.transform.name == ltname) |
|
|
|
|
return item.point.y; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//if (Physics.Raycast(position, Vector3.down, out hit, 100f, 15)) |
|
|
|
|
//{ |
|
|
|
|
// // 如果射线击中地面,设置Y轴为地面高度 |
|
|
|
|
// return hit.point.y; |
|
|
|
|
//} |
|
|
|
|
return 0f; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|