1.随机时间刷新车辆
2.车辆AI完善
3.滚动监视视窗
master
GamerHJD 3 years ago
parent ceecebd962
commit c1e9ea71ac

@ -31,7 +31,7 @@ AnimationClip:
outWeight: 0.33333334
- serializedVersion: 3
time: 0.6666667
value: -47
value: -560
inSlope: 0
outSlope: 0
tangentMode: 136
@ -201,7 +201,7 @@ AnimationClip:
outWeight: 0.33333334
- serializedVersion: 3
time: 0.6666667
value: -47
value: -560
inSlope: 0
outSlope: 0
tangentMode: 136

@ -6,7 +6,7 @@ RenderTexture:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: New Render Texture
m_Name: Camera01
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000

@ -0,0 +1,37 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!84 &8400000
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Camera02
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 256
m_Height: 256
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 2
m_ColorFormat: 8
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 0
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1

File diff suppressed because it is too large Load Diff

@ -14,8 +14,8 @@ public static class Const
public static float CarTurnSpeed = 5f;
//刷车最小/最大时间
public static int RefreshCarminTime = 3;
public static int RefreshCarmaxTime = 11;
public static int RefreshCarminTime = 1;
public static int RefreshCarmaxTime = 10;

@ -15,6 +15,8 @@ public class GameRoot : MonoBehaviour
//信息窗口
public InfoWnd infoWnd;
//菜单按钮窗口
public MenuWnd menuWnd;
private void Awake()
{
@ -26,17 +28,24 @@ public class GameRoot : MonoBehaviour
//车辆流量监控初始化
roadSys = GetComponent<RoadSys>();
roadSys.Init();
//UI窗口初始化
menuWnd.Init();
//定时任务服务
pt = new PETimer();
}
private void Start()
{
StartCoroutine(carSys.RandomTimeCreat());
}
private void Update()
{
pt.Update();
carSys.CreatCar();//每n秒随机出发点创建汽车
roadSys.RoadStateUpdate(3);//每n秒检测路段状态

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

@ -11,10 +11,7 @@ public class MenuWnd : MonoBehaviour
public GameObject threeDcamera;
public GameObject twoDcamera;
// Start is called before the first frame update
void Start()
{
Init();
}
public void Init()

Loading…
Cancel
Save