void SpawnCrowd() { int startIndex = 0; if (previousSelection == selectedOption) { startIndex = spawnedObjects.Count; int toRemove = spawnedObjects.Count - sizeOfCrowd; if (toRemove > 0) { for (int i = 0; i < toRemove; i++) { if (spawnedObjects[i]) { Destroy(spawnedObjects[i]); } } spawnedObjects.RemoveRange(0, toRemove); } } else { foreach (var obj in spawnedObjects) { if (obj) { Destroy(obj); } } spawnedObjects.Clear(); } previousSelection = selectedOption; Vector3 center = transform.position; for (int i = startIndex; i < sizeOfCrowd; i++) { Vector3 rand = Random.onUnitSphere * radius; Vector3 position = center + new Vector3(rand.x * radiusScaler.x, 0, rand.z * radiusScaler.y); float disFromCenter = Vector3.Distance(center, position); if (disFromCenter < slopeStart) { position.y = center.y; } else { position.y = (disFromCenter - slopeStart) / (radius - slopeStart) * slopeAmount; } RaycastHit hit; if (Physics.Raycast(position + Vector3.up * 10, Vector3.down, out hit, 50, -1)) { position.y = hit.point.y; } var g = Instantiate(options[selectedOption], transform); g.transform.position = position;// + new Vector3(Random.value, 0, Random.value) * 2; g.transform.LookAt(new Vector3(0, position.y, 0), Vector3.up); g.transform.localScale = baseScale + Vector3.one * Random.value * 0.25f; if (g.GetComponent <Animator>()) { g.GetComponent <Animator>().speed = Random.Range(0.9f, 1.1f); g.GetComponent <Animator>().SetInteger("Anim", Random.Range(0, 4)); } else if (g.GetComponent <MeshAnimatorBase>()) { MeshAnimatorBase ma = g.GetComponent <MeshAnimatorBase>(); if (meshAnimationNames.Length > 0) { string name = meshAnimationNames[Random.Range(0, meshAnimationNames.Length)]; for (int a = 0; a < ma.animations.Length; a++) { if (ma.animations[a].AnimationName == name) { ma.defaultAnimation = ma.animations[a]; ma.Play(a); break; } } ma.speed = Random.Range(0.9f, 1.1f); } ma.SetTimeNormalized(Random.value, true); } spawnedObjects.Add(g); } }