/// <summary> /// Adds a pedestrian to the simulation /// </summary> public void AddPedestrian() { // Yes, it is a hack. Deal with it if (!mPedestriansParent) { mPedestriansParent = new GameObject(); mPedestriansParent.name = "Pedestrians"; } GameObject pedestrian = GameObject.Instantiate(PedestrianPrefab); pedestrian.name = "AIPedestrian_" + mAgents.Count; pedestrian.transform.SetParent(mPedestriansParent.transform); Vector3 position = AStarSearch.GetRandomWaypoint(WaypointsExample.PedestriansGraph); position.y = 0.0f; pedestrian.transform.position = position; // Init the pedestrian behaviour pedestrian.GetComponent <PedestrianBehavior>().Init(); pedestrian.GetComponent <NN.NeuralNetwork>().Init(); if (mNNController == null) { mNNController = GameObject.FindGameObjectWithTag("Neural Network Controller").GetComponent <NN.NeuralNetwork>(); mNNController.Init(); } pedestrian.GetComponent <NN.NeuralNetwork>().SetConnectionWeights(mNNController.GetConnectionWeights()); mAgents.Add(pedestrian.GetComponent <PedestrianBehavior>()); }
void Update() { if (Vector3.Distance(transform.position, mNextStep) > mMinDistance) { transform.position = Vector3.MoveTowards(transform.position, mNextStep, Time.deltaTime * mVelocity); } else { if (mPath.Count > 0) { mPath.RemoveAt(mPath.Count - 1); } if (mPath.Count > 0) { mNextStep = mPath[mPath.Count - 1]; } else { transform.position = mNextStep; Vector3 start = AStarSearch.GetNearestWaypoint(WaypointsExample.CarsGraph, transform.position); Vector3 end = AStarSearch.GetRandomWaypoint(WaypointsExample.CarsGraph); mPath = AStarSearch.FindNewObjective(WaypointsExample.CarsGraph, start, end); if (mPath.Count > 0) { mNextStep = mPath[mPath.Count - 1]; } } } }
void GetNewPath() { Vector3 start = AStarSearch.GetNearestWaypoint(WaypointsExample.PedestriansGraph, transform.position); Vector3 end = AStarSearch.GetRandomWaypoint(WaypointsExample.PedestriansGraph); mPath = AStarSearch.FindNewObjective(WaypointsExample.PedestriansGraph, start, end); mPath.Add(start); }
void Start() { Vector3 start = AStarSearch.GetNearestWaypoint(WaypointsExample.CarsGraph, transform.position); Vector3 end = AStarSearch.GetRandomWaypoint(WaypointsExample.CarsGraph); mPath = AStarSearch.FindNewObjective(WaypointsExample.CarsGraph, start, end); if (mPath.Count > 0) { mNextStep = mPath[mPath.Count - 1]; } }
void CreateCars() { //Generate the AI cars GameObject cars = new GameObject(); cars.name = "Cars"; for (int i = 0; i < CarAmount; ++i) { GameObject car = GameObject.Instantiate(Car); car.name = "AICar_" + i; car.transform.SetParent(cars.transform); car.transform.position = AStarSearch.GetRandomWaypoint(WaypointsExample.CarsGraph); } }