IEnumerator SpawnTry(RabbitAI mother, RabbitAI father) { LayerMask mask = LayerMask.GetMask(new string[] { "Herbivore", "Carnivore", "Food" }); Vector3 randomPoint = mother.transform.position + Random.insideUnitSphere.normalized * Random.Range(1.3f, 2.5f); NavMeshHit hit; BaseAIPersonality fatherPersonality = father.Personality; // Storing father personality in case father dies; int tries = 0; while (tries < 500) { if (!mother) { yield break; // mother died before giving birth } randomPoint = mother.transform.position + Random.insideUnitSphere.normalized * Random.Range(1.3f, 2.5f); if (NavMesh.SamplePosition(randomPoint, out hit, 1f, NavMesh.AllAreas)) { if (Physics.OverlapSphereNonAlloc(hit.position, .3f, null, mask) <= 0) { GameObject child = Instantiate(m_RabbitChildPrefab, mother.transform.parent); child.transform.position = hit.position; child.GetComponent <RabbitAI>().SetParents(mother.Personality, fatherPersonality); yield break; } } tries++; yield return(new WaitForSeconds(0.1f)); } }
public static BaseAIPersonality Child(BaseAIPersonality parent1, BaseAIPersonality parent2) { BaseAIPersonality newPersonality = new BaseAIPersonality(); newPersonality.HungryThreshold = UnityEngine.Random.Range(0f, 1f) > .5f ? parent1.HungryThreshold : parent2.HungryThreshold; newPersonality.HungerDecayRatio = UnityEngine.Random.Range(0f, 1f) > .5f ? parent1.HungerDecayRatio : parent2.HungerDecayRatio; newPersonality.NeedDetection = UnityEngine.Random.Range(0f, 1f) > .5f ? parent1.NeedDetection : parent2.NeedDetection; newPersonality.HealthDecayRatio = UnityEngine.Random.Range(0f, 1f) > .5f ? parent1.HealthDecayRatio : parent2.HealthDecayRatio; return(newPersonality); }
public virtual void SetParents(BaseAIPersonality parent1, BaseAIPersonality parent2) { m_Personality = BaseAIPersonality.Child(parent1, parent2); }