|
|
@ -21,7 +21,8 @@ public class CarSys : MonoBehaviour
|
|
|
|
public Dictionary<int, GameObject> carList = new Dictionary<int, GameObject>();
|
|
|
|
public Dictionary<int, GameObject> carList = new Dictionary<int, GameObject>();
|
|
|
|
//
|
|
|
|
//
|
|
|
|
public int CarID = 0;
|
|
|
|
public int CarID = 0;
|
|
|
|
private bool canCreat = true;
|
|
|
|
|
|
|
|
|
|
|
|
public bool canCreat = false;
|
|
|
|
|
|
|
|
|
|
|
|
//移动点列表
|
|
|
|
//移动点列表
|
|
|
|
private Dictionary<string, Transform> MoveList = new Dictionary<string, Transform>();
|
|
|
|
private Dictionary<string, Transform> MoveList = new Dictionary<string, Transform>();
|
|
|
@ -37,6 +38,7 @@ public class CarSys : MonoBehaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region CarAI
|
|
|
|
//检索移动点
|
|
|
|
//检索移动点
|
|
|
|
public void CheckBornoint()
|
|
|
|
public void CheckBornoint()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -55,22 +57,22 @@ public class CarSys : MonoBehaviour
|
|
|
|
for (int i = 0; i < moveObjs.Length; i++)
|
|
|
|
for (int i = 0; i < moveObjs.Length; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MoveList.Add(moveObjs[i].name, moveObjs[i].transform);
|
|
|
|
MoveList.Add(moveObjs[i].name, moveObjs[i].transform);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//车辆AI移动函数
|
|
|
|
//车辆AI移动函数
|
|
|
|
public IEnumerator CarMove(string lineName,NavMeshAgent agent)
|
|
|
|
public IEnumerator CarMove(string lineName, NavMeshAgent agent)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (lineName != "" && agent != null)
|
|
|
|
if (lineName != "" && agent != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Transform moveListParent = null;
|
|
|
|
Transform moveListParent = null;
|
|
|
|
MoveList.TryGetValue(lineName,out moveListParent);
|
|
|
|
MoveList.TryGetValue(lineName, out moveListParent);
|
|
|
|
Drive drive = agent.GetComponent<Drive>();
|
|
|
|
Drive drive = agent.GetComponent<Drive>();
|
|
|
|
if (moveListParent != null)
|
|
|
|
if (moveListParent != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Transform[] moveList=new Transform[moveListParent.childCount];
|
|
|
|
Transform[] moveList = new Transform[moveListParent.childCount];
|
|
|
|
//遍历移动列表
|
|
|
|
//遍历移动列表
|
|
|
|
for (int i = 0; i < moveListParent.childCount; i++)
|
|
|
|
for (int i = 0; i < moveListParent.childCount; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -81,7 +83,7 @@ public class CarSys : MonoBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
float t = 0;
|
|
|
|
float t = 0;
|
|
|
|
//向移动列表支点移动
|
|
|
|
//向移动列表支点移动
|
|
|
|
agent.SetDestination(moveList[i].position);
|
|
|
|
agent.SetDestination(moveList[i].position);
|
|
|
|
//携程检测距离与路况
|
|
|
|
//携程检测距离与路况
|
|
|
|
while (true)
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -91,48 +93,48 @@ public class CarSys : MonoBehaviour
|
|
|
|
//前方车辆安全距离检测
|
|
|
|
//前方车辆安全距离检测
|
|
|
|
if (drive.drivetime > Const.SafeCheckTime)
|
|
|
|
if (drive.drivetime > Const.SafeCheckTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
drive.isnormalDrive = CheckFrontCar(drive) ? false : true;
|
|
|
|
drive.isnormalDrive = CheckFrontCar(drive) ? false : true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(drive.isnormalDrive == false)
|
|
|
|
if (drive.isnormalDrive == false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
agent.velocity = Vector3.zero;
|
|
|
|
agent.velocity = Vector3.zero;
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float distance = Vector3.Distance(agent.transform.position, moveList[i].position);
|
|
|
|
float distance = Vector3.Distance(agent.transform.position, moveList[i].position);
|
|
|
|
|
|
|
|
|
|
|
|
//终点检测
|
|
|
|
//终点检测
|
|
|
|
if (distance <= 0.4f)
|
|
|
|
if (distance <= 0.4f)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
agent.velocity= Vector3.zero;
|
|
|
|
agent.velocity = Vector3.zero;
|
|
|
|
t = 0;
|
|
|
|
t = 0;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//弯道减速
|
|
|
|
//弯道减速
|
|
|
|
if (distance <= 20f&&distance>0.4f)
|
|
|
|
if (distance <= 20f && distance > 0.4f)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
drive.dirveState = DriveState.Turn;
|
|
|
|
drive.dirveState = DriveState.Turn;
|
|
|
|
agent.velocity = Dir* 15;
|
|
|
|
agent.velocity = Dir * 15;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
drive.dirveState = DriveState.Line;
|
|
|
|
drive.dirveState = DriveState.Line;
|
|
|
|
t += Time.deltaTime * Const.CarTurnSpeed;
|
|
|
|
t += Time.deltaTime * Const.CarTurnSpeed;
|
|
|
|
agent.velocity = Vector3.Lerp(Vector3.zero,Dir*20,t);
|
|
|
|
agent.velocity = Vector3.Lerp(Vector3.zero, Dir * 20, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//回收车辆
|
|
|
|
//回收车辆
|
|
|
|
carList.Remove(drive.carId);
|
|
|
|
carList.Remove(drive.carId);
|
|
|
|
Destroy(agent.gameObject);
|
|
|
|
Destroy(agent.gameObject);
|
|
|
|
GameRoot.Instance.infoWnd.SetCarText(-1);
|
|
|
|
GameRoot.Instance.infoWnd.SetCarText(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -142,22 +144,52 @@ public class CarSys : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator RandomTimeCreatCar(float time,Transform parent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//检测前方车辆
|
|
|
|
|
|
|
|
public bool CheckFrontCar(Drive drive)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (drive.FrontCar == null) return false;
|
|
|
|
if (timer < time) {
|
|
|
|
|
|
|
|
canCreat = false;
|
|
|
|
if (Vector3.Distance(drive.transform.position, drive.FrontCar.transform.position) < Const.SafeDistance)
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
canCreat = true;
|
|
|
|
|
|
|
|
timer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canCreat == false) yield break;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#region MyRegion
|
|
|
|
|
|
|
|
//for (int i = 0; i < carList.Count; i++)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// Vector3 target = carList[i].transform.position;
|
|
|
|
|
|
|
|
// Vector3 dir = target - drive.transform.position;
|
|
|
|
|
|
|
|
// drive.dot = Vector3.Dot(drive.transform.forward.normalized,dir.normalized);
|
|
|
|
|
|
|
|
// //判断是否前方有小于安全距离的车辆
|
|
|
|
|
|
|
|
// if (Vector3.Dot(drive.transform.forward.normalized, dir.normalized) > Mathf.Cos(Mathf.PI/6) && Vector3.Distance(target, drive.transform.position) <= Const.SafeDistance)
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// Debug.Log(drive.gameObject.name);
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//return false;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 汽车随机生成
|
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log(time);
|
|
|
|
public void CreatCar(Transform parent)
|
|
|
|
yield return new WaitForSeconds(time);
|
|
|
|
{
|
|
|
|
// 随机创建车辆种类
|
|
|
|
// 随机创建车辆种类
|
|
|
|
int carIndex = Random.Range(0, carTypeList.Length - 1);
|
|
|
|
int carIndex = Random.Range(0, carTypeList.Length - 1);
|
|
|
|
//实例化车辆
|
|
|
|
//实例化车辆
|
|
|
@ -183,69 +215,41 @@ public class CarSys : MonoBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
StartCoroutine(CarMove(parent.name, agent));
|
|
|
|
StartCoroutine(CarMove(parent.name, agent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int temp = 0;
|
|
|
|
public IEnumerator RandomTimeCreat()
|
|
|
|
//汽车生成函数
|
|
|
|
|
|
|
|
public void CreatCar()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//计时
|
|
|
|
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//随机获取出生点
|
|
|
|
//随机获取出生点
|
|
|
|
Transform parent = null;
|
|
|
|
Transform parent = null;
|
|
|
|
int pointIndex= Random.Range(1, BornList.Count);
|
|
|
|
int pointIndex = Random.Range(1, BornList.Count);
|
|
|
|
BornList.TryGetValue("0" + pointIndex.ToString(),out parent);
|
|
|
|
BornList.TryGetValue("0" + pointIndex.ToString(), out parent);
|
|
|
|
//创建车辆
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
if (parent != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//随机时间
|
|
|
|
int t = Random.Range(Const.RefreshCarminTime,Const.RefreshCarmaxTime);
|
|
|
|
int t = Random.Range(Const.RefreshCarminTime, Const.RefreshCarmaxTime);
|
|
|
|
|
|
|
|
Debug.Log(t);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
|
|
if (t == temp) return;
|
|
|
|
if (timer >= t)
|
|
|
|
temp = t;
|
|
|
|
{
|
|
|
|
|
|
|
|
timer = 0;
|
|
|
|
StartCoroutine(RandomTimeCreatCar(t,parent));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//检测前方车辆
|
|
|
|
|
|
|
|
public bool CheckFrontCar(Drive drive)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (drive.FrontCar==null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Vector3.Distance(drive.transform.position, drive.FrontCar.transform.position) < Const.SafeDistance)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#region MyRegion
|
|
|
|
|
|
|
|
//for (int i = 0; i < carList.Count; i++)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// Vector3 target = carList[i].transform.position;
|
|
|
|
|
|
|
|
// Vector3 dir = target - drive.transform.position;
|
|
|
|
|
|
|
|
// drive.dot = Vector3.Dot(drive.transform.forward.normalized,dir.normalized);
|
|
|
|
|
|
|
|
// //判断是否前方有小于安全距离的车辆
|
|
|
|
|
|
|
|
// if (Vector3.Dot(drive.transform.forward.normalized, dir.normalized) > Mathf.Cos(Mathf.PI/6) && Vector3.Distance(target, drive.transform.position) <= Const.SafeDistance)
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// Debug.Log(drive.gameObject.name);
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
CreatCar(parent);
|
|
|
|
// else
|
|
|
|
|
|
|
|
// {
|
|
|
|
}
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
|
|
StartCoroutine(RandomTimeCreat());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//return false;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|