public void Init() { dna = new DNA_2(dnaLength, 6); character = GetComponent <ThirdPersonCharacter>(); //The reset properties of the characters. timeAlive = 0.0f; dead = false; startPosition = gameObject.transform.position; }
public void Combine(DNA_2 parent1, DNA_2 parent2) { //Split the DNA into two parts. We get one part from the first parent and the other from the second parent //Since this example only uses DNA of length = 1, we always get the DNA from the first parent only for (int i = 0; i < dnaLength; i++) { if (i < dnaLength / 2.0f) { //Get the reespective gene from the parent 1 Genes[i] = parent1.Genes[i]; } else { //Get the respective gene from the parent 2 Genes[i] = parent2.Genes[i]; } } }
void Breed(DNA_2 parent1, DNA_2 parent2) { Vector3 randomPosition = new Vector3( spawnPoint.transform.position.x + Random.Range(-2, 2), spawnPoint.transform.position.y, spawnPoint.transform.position.z + Random.Range(-2, 2)); GameObject offspring = Instantiate(personPrefab, randomPosition, Quaternion.identity); offspring.GetComponent <Brain>().Init(); if (Random.Range(0, 100) < 2) { offspring.GetComponent <Brain>().dna.Mutate(); } else { offspring.GetComponent <Brain>().dna.Combine(parent1, parent2); } population.Add(offspring); }