- Home /
Generate gameObject horde, more positioned towards front of horde
I am trying to create a non-uniform random range of enemy characters in a scene. I want there to be more enemies towards the front of the horde and less trailing off towards the back of the horde. How would I do something like this? I figured maybe create an empty gameObject and have the generated enemies target it like
 public Vector3 target : Transform;
 if (target == null && GameObject.FindWithTag("Empty"))
 {
          target = GameObject.FindWithTag("Empty").transform;
 }
But I feel like that would not work. Does anyone have any ideas?
Here is my method for spawning the horde
     void SpawnHorde()
     {
         for (int i = 0; i < hordeCount; i++) 
         {
             Vector3 spawnPosition = new Vector3(Random.Range (-location.x, location.x), location.y, location.z);
             GameObject obj = Instantiate(horde, spawnPosition, Quaternion.identity) as GameObject;
         }
     }
Maybe set bounds for the location?? Ugh I'm stumped
here is an image of how I want to generate them: 
Hey, you ever figure that out? I am trying to do the same thing right now and I am completely stumped.
Yes I figured it out. Lol, are you in IG$$anonymous$$E 202?
Yeah I am, haha
Thank you for the answer, it's making more sense now, I got the other parts of the assignment down, just was having trouble with this one. :D
Answer by corinne44 · Sep 19, 2017 at 07:14 PM
 // Name
 // NonUniform - A class that generate a larger number of characters whose position is based upon non-uniform random values.
 // No known errors. For the purpose of this assignment, variables have been made private and their values have been hardcoded.
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NonUniform : MonoBehaviour 
 {
     private int hordeCount; // Number of horde Prefabs to generate, 50-100
     private List<string> collisions = new List<string>(); // A list of strings containing all the detected collisions
 
     // Methods
     // Use this for initialization
     void Start () 
     {
         SpawnHorde ();
     }
 
     void SpawnHorde()
     {
         int hordeCount = 100;
 
         float zPosition = 0;
 
         const int maxInColumn = 10;
 
         while (hordeCount > 0) 
         {
             int numberInCol = Random.Range (5, maxInColumn);
             hordeCount -= numberInCol;
 
             if(hordeCount < 0)
             {
                 numberInCol += hordeCount;
             }
 
             for(int i = 0; i < numberInCol; i++)
             {
                 int x = Random.Range (0, 200);
                 float z = 40 + zPosition;
                 float y = Terrain.activeTerrain.SampleHeight (new Vector3 (x, 0, z)) + Terrain.activeTerrain.GetPosition().y;
                 Vector3 spawnPosition = new Vector3(Random.Range (0, 200), y, 40 + zPosition);
                 Instantiate(Resources.Load ("Prefabs/Mum"), spawnPosition, Quaternion.identity);
             }
             zPosition += (float)maxInColumn * 20f / (float)hordeCount;
         }
     }
 
     /// <summary>
     /// Raises the collison enter event; checks to make sure Gaussian Leads are not colliding with any other models.
     /// Although I realize that since this script is attached to the Terrain and not a gameObject, it doesn't work as desired.
     /// </summary>
     /// <param name="col">Col.</param>
     void OnCollisonEnter(Collision col)
     {
         collisions.Add (col.gameObject.name);
         if (collisions.Contains ("Mummy") || collisions.Contains ("Barrel2") || collisions.Contains ("Lara")) 
         {
             Destroy (gameObject);
         }
     }
 
     void OnCollisionExit(Collision col)
     {
         collisions.Remove (col.gameObject.name); // When the objects are no longer colliding, remove them from the list of detected collisions
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                