private GameObject Breed(GameObject parent1, GameObject parent2) { Vector3 pos = new Vector3(Random.Range(-9f, 9f), Random.Range(-4.5f, 4.5f), 0); GameObject offspring = Instantiate(personPrefab, pos, Quaternion.identity); ColorDNA dna1 = parent1.GetComponent <ColorDNA>(); ColorDNA dna2 = parent2.GetComponent <ColorDNA>(); ColorDNA offspringDna = offspring.GetComponent <ColorDNA>(); if (Random.Range(0, 1000) > 5) { //Swap parent dna offspringDna.r = Random.Range(0, 10) < 5 ? dna1.r : dna2.r; offspringDna.g = Random.Range(0, 10) < 5 ? dna1.g : dna2.g; offspringDna.b = Random.Range(0, 10) < 5 ? dna1.b : dna2.b; offspringDna.s = Random.Range(0, 10) < 5 ? dna1.s : dna2.s; } else { //Mutation dna offspringDna.r = Random.Range(0f, 1f); offspringDna.g = Random.Range(0f, 1f); offspringDna.b = Random.Range(0f, 1f); offspringDna.s = Random.Range(0.1f, 0.3f); } return(offspring); }
private void Start() { for (int i = 0; i < populationSize; i++) { Vector3 pos = new Vector3(Random.Range(-9f, 9f), Random.Range(-4.5f, 4.5f), 0); GameObject go = Instantiate(personPrefab, pos, Quaternion.identity); ColorDNA goDNA = go.GetComponent <ColorDNA>(); goDNA.r = Random.Range(0f, 1.0f); goDNA.g = Random.Range(0f, 1.0f); goDNA.b = Random.Range(0f, 1.0f); goDNA.s = Random.Range(0.1f, 0.5f); populationList.Add(go); } }