public static SwarmController CreateNew(Transform spawnPosition, Transform swarmControllerPrefab, Transform swarmMemberPrefab, List <Character> characters, SwarmMemberConfig config) { var swarmPrefab = Instantiate(swarmControllerPrefab, swarmControllerPrefab.position, Quaternion.identity); var swarmController = swarmPrefab.GetComponent <SwarmController>(); var destinationSetter = swarmController.GetComponent <AIDestinationSetter>(); swarmController.Init(spawnPosition, characters, config); var closestCharacter = swarmController.FindClosestSquadMember(); destinationSetter.target = closestCharacter.transform; swarmController.currentTarget = closestCharacter.transform; swarmController.spawnPosition = spawnPosition; swarmController.memberPrefab = swarmMemberPrefab; for (int i = 0; i < config.swarmMaxLimit; i++) { var swarmMember = SwarmMember.CreateNew(swarmMemberPrefab, swarmController, config, swarmController.currentTarget); swarmController.swarmMembers.Add(swarmMember); } return(swarmController); }
private void FixedUpdate() { if (currentTarget == null) { currentTarget = FindClosestSquadMember().transform; foreach (var member in swarmMembers) { member.SetTarget(currentTarget); } } //Reset and respawn if (swarmMembers.Count < config.swarmRespawnSize) { transform.position = spawnPosition.position; for (int i = swarmMembers.Count; i < config.swarmMaxLimit; i++) { var swarmMember = SwarmMember.CreateNew(memberPrefab, this, config, this.currentTarget); swarmMembers.Add(swarmMember); } } }