public void Spawn(int id, int subId) { float distance = Random.value * 0.8f + 0.1f; TrafEntry entry = system.GetEntry(id, subId); if (entry == null) { return; } InterpolatedPosition pos = entry.GetInterpolatedPosition(distance); if (!Physics.CheckSphere(pos.position, checkRadius, 1 << LayerMask.NameToLayer("Traffic"))) { GameObject go = GameObject.Instantiate(prefabs[Random.Range(0, prefabs.Length)], pos.position, Quaternion.identity) as GameObject; TrafAIMotor motor = go.GetComponent <TrafAIMotor>(); go.layer = 16; motor.currentIndex = pos.targetIndex; motor.currentEntry = entry; go.transform.LookAt(entry.waypoints[pos.targetIndex]); motor.system = system; motor.Init(); } }
public void SpawnFixed() { GameObject prefab = fixedPrefabs[Random.Range(0, fixedPrefabs.Length)]; var pMotor = prefab.GetComponent <TrafAIMotor>(); int index = Random.Range(0, pMotor.fixedPath.Count); int id = pMotor.fixedPath[index].id; int subId = pMotor.fixedPath[index].subId; float distance = Random.value * 0.8f + 0.1f; TrafEntry entry = system.GetEntry(id, subId); if (entry == null) { return; } InterpolatedPosition pos = entry.GetInterpolatedPosition(distance); if (!Physics.CheckSphere(pos.position, checkRadius * 3, 1 << LayerMask.NameToLayer("Traffic"))) { GameObject go = GameObject.Instantiate(prefab, pos.position, Quaternion.identity) as GameObject; TrafAIMotor motor = go.GetComponent <TrafAIMotor>(); motor.currentIndex = pos.targetIndex; motor.currentEntry = entry; go.transform.LookAt(entry.waypoints[pos.targetIndex]); motor.system = system; motor.Init(); motor.currentFixedNode = index; } }
//ANTONELLO private void guidaAutomaticaSF() { //Vector3 tempPos = transform.position; int id = Random.Range(0, maxIdent); int subId = Random.Range(0, maxSub); //float distance = Random.value * 0.8f + 0.1f; GameObject go = ottieniRiferimentoPlayer(); TrackController trackController = TrackController.Instance; TrafEntry entry = trackController.GetCurrentTrafEntry(); //-> ottengo la entry corrente //TrafEntry entryOk = null; //lo scopo della funziona calcolaDistanza è settare la variabile distance che farà si che verrà settato correttamente il waypoint target float distance = calcolaDistanza(entry, go); //entry = system.GetEntry(id, subId); //-> serve a ottenere la entry di un determinato punto, bisogna indicare il giusto id e subid if (entry == null) { Debug.Log("Entry = null"); return; } InterpolatedPosition pos = entry.GetInterpolatedPosition(distance); if (!Physics.CheckSphere(pos.position, checkRadius, 1 << LayerMask.NameToLayer("Traffic"))) { GameObject nose = new GameObject("nose"); nose.transform.SetParent(go.transform); nose.transform.localPosition = new Vector3(0, 0.5f, 2f); nose.transform.localRotation = Quaternion.identity; nose.transform.localScale = new Vector3(2f, 2f, 2f); TrafAIMotor motor = go.AddComponent <TrafAIMotor>(); GameObject colliderOstacoli = new GameObject("colliderOstacoli"); colliderOstacoli.transform.SetParent(go.transform); BoxCollider boxColliderOstacoli = colliderOstacoli.AddComponent <BoxCollider>(); boxColliderOstacoli.isTrigger = true; colliderOstacoli.transform.localPosition = new Vector3(0f, 0.65f, 7f); colliderOstacoli.transform.localScale = new Vector3(1f, 1f, 1f); colliderOstacoli.transform.localRotation = Quaternion.identity; boxColliderOstacoli.size = new Vector3(3f, 0.75f, 10f); TrafAIMotor.GestoreCollisioni gestore = colliderOstacoli.AddComponent <TrafAIMotor.GestoreCollisioni>(); gestore.setMotor(motor); //L'ISTRUZIONE SOTTO FA TELETRASPORTARE L'AUTO NELLA POSIZIONE pos.position //go.transform.position = pos.position; //go.AddComponent<TrafWheels>(); motor.currentIndex = pos.targetIndex; motor.currentEntry = entry; //L'istruzione sotto ruota la macchina in direzione del waypoint target //go.transform.LookAt(entry.waypoints[pos.targetIndex]); motor.system = system; motor.nose = nose; motor.raycastOrigin = nose.transform; motor.targetHeight = 0f; motor.waypointThreshold = 3f; guidaAutomatica = true; /*motor.fixedRoute = true; * * //RoadGraphEdge edge = new RoadGraphEdge(); * //edge.id = 5; edge.subId = 0; * RoadGraphEdge edge1 = new RoadGraphEdge(); * edge1.id = 1003; edge1.subId = 4; * RoadGraphEdge edge2 = new RoadGraphEdge(); * edge2.id = 3; edge2.subId = 1; * //RoadGraphEdge edge3 = new RoadGraphEdge(); * //edge3.id = 4; edge3.subId = 3; * * List<RoadGraphEdge> listaEdge = new List<RoadGraphEdge>(); * //listaEdge.Add(edge); * listaEdge.Add(edge1); * listaEdge.Add(edge2); * //listaEdge.Add(edge3); * motor.fixedPath = listaEdge;*/ motor.Init(); } else { guidaAutomaticaSF(); //è una ricorsione, fa si che si ripete la funzione finchè tutto vada bene } }
public GameObject Spawn(TrafEntry entry, bool unseenArea = false, CarAIController sameCar = null) { if (entry == null) { return(null); } float distance = Random.value * 0.8f + 0.1f; InterpolatedPosition pos = entry.GetInterpolatedPosition(distance); if (!Physics.CheckSphere(pos.position, checkRadius, NPCSpawnCheckBitmask)) { GameObject go; if (sameCar != null) //If it is the same car simply reposition it { go = sameCar.gameObject; go.transform.position = pos.position + Vector3.up; } else { go = GameObject.Instantiate(prefabs[Random.Range(0, prefabs.Length)], pos.position + Vector3.up * 3.5f, Quaternion.identity) as GameObject; } go.transform.LookAt(entry.waypoints[pos.targetIndex]); go.transform.SetParent(this.transform); // Put all spawned cars under this object for better organization var carAI = go.GetComponent <CarAIController>(); if (unseenArea) { if (!CheckSilentRespawnEligibility(carAI, Camera.main)) { if (sameCar == null) { DestroyImmediate(go); } return(null); } } else if (sameCar == null) //If it is a new spawn { //assign userid if (trafInfoManager.freeIdPool.Count == 0) { carAI.carID = trafInfoManager.GeneratePseudoRandomUID(); } else { carAI.carID = trafInfoManager.freeIdPool.Dequeue(); } carAI.RandomSelectCarPaintTexture(); ++totalTrafficCarCount; if (trafPerfManager != null) { trafPerfManager.AddAICar(carAI); } } // Init or Reinit AI motor TrafAIMotor motor = go.GetComponent <TrafAIMotor>(); motor.system = system; motor.Init(pos.targetIndex, entry); // Init or Reinit AI Controller carAI.Init(); return(go); } else { return(null); } }