上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PathFinding : MonoBehaviour
{
public Transform PathParent;
public List<Transform> PathList = new List<Transform>();
public int NowIndex = 0;
// Start is called before the first frame update
void Start()
{
foreach (Transform item in PathParent)
{
if (item != PathParent)
PathList.Add(item);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
if (NowIndex == 0)
{
LocationData data = new LocationData();
data.targetNo = 102;
data.xAxis = PathList[0].transform.position.x;
data.yAxis = PathList[0].transform.position.y;
data.zAxis = PathList[0].transform.position.z;
data.stations = PathList[0].name;
var agent = GetComponent<LocationSimulator>().CreateAgent(data);
agent.enabled = false;
agent.GetComponent<NavMeshAgent>().enabled = false;
GetComponent<LocationSimulator>().agentControllers.Add(data.targetNo, agent);
Vector3 pos = ConversionTransform(data);
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos;
}
else if (NowIndex < PathList.Count)
{
LocationData data = new LocationData();
data.targetNo = 102;
data.stations = PathList[NowIndex].name;
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);
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos;
}
else
{
NowIndex = 0;
}
NowIndex++;
}
}
/// <summary>
/// 根据数据转换到Unity坐标
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public Vector3 ConversionTransform(LocationData data)
{
//判断数据被检测到的区域
string St1 = "1";//楼梯入口基站
string St2 = "2";//楼梯间基站
string St3 = "3";//楼梯底层基站
//纠正坐标
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;
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 = -21.1f;
}
else if (data.stations.Contains(St1) && data.stations.Contains(St2))
{ //-4到-5楼梯上半段
pos.y = SetY(pos);
}
else if (data.stations.Contains(St2) && data.stations.Contains(St3))
{ //-4到-5楼梯下半段
pos.y = SetY(pos);
}
else if (data.stations.Contains(St2) && !data.stations.Contains(St1) && !data.stations.Contains(St3))
{
//-4到-5楼梯间过度
pos.y = -19.3f;
}
return pos;
}
/// <summary>
/// 确保角色始终在地面上
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public float SetY(Vector3 position)
{
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"))
return item.point.y;
}
}
if (Physics.Raycast(position, Vector3.down, out hit, 100f, 15))
{
// 如果射线击中地面,设置Y轴为地面高度
return hit.point.y;
}
return 0f;
}
}