c# - How to Position an Object Separately from Randomized Objects in Unity? -


in 2d game have randomized objects spawned 4 5 clones each time run game. problem have different object want spawn 1 clone, , position appear after last clone of randomized objects have in game.

the objects randomization works in game, need separate object want spawned independently , after last clone of randomized objects.

this code using 1 line of attempt spawn independent object: (the code taken this tutorial)

 using unityengine;     using system;     using system.collections.generic;       //allows use lists.     using random = unityengine.random;      //tells random use unity engine random number generator.      namespace completed      {          public class boardmanager : monobehaviour         {              // using serializable allows embed class sub properties in inspector.             [serializable]             public class count             {                 public int minimum;             //minimum value our count class.                 public int maximum;             //maximum value our count class.                   //assignment constructor.                 public count (int min, int max)                 {                     minimum = min;                     maximum = max;                 }             }               public int columns = 7;                                         //number of columns in our game board.             public count random1count = new count (1, 2);                       //lower , upper limit our random number of objects             public count random2count = new count (1, 1);             public count random3count = new count (1, 1);             public count random4count = new count (1, 1);              public gameobject[] randomobject1;                                  //array of objects prefabs.             public gameobject[] randomobject2;             public gameobject[] randomobject3;             public gameobject[] randomobject4;              public gameobject obj; // independent object declaration              private list <vector3> gridpositions = new list <vector3> ();   //a list of possible locations place objects.               //clears our list gridpositions , prepares generate new board.             void initialiselist ()             {                 //clear our list gridpositions.                 gridpositions.clear ();                  //loop through x axis (columns).                 for(int x = 2; x < columns; x++)                 {                       //at each index add new vector3 our list x , y coordinates of position.                     gridpositions.add (new vector3(x, 0.3f, 0f));                      instantiate(obj); // attempt instantiate separate object                     debug.log(obj.transform.position.x); // attempt track position of separate object                 }              }               //randomposition returns random position our list gridpositions.             vector3 randomposition ()             {                 //declare integer randomindex, set it's value random number between 0 , count of items in our list gridpositions.                 int randomindex = random.range (0, gridpositions.count);                  //declare variable of type vector3 called randomposition, set it's value entry @ randomindex our list gridpositions.                 vector3 randomposition = gridpositions[randomindex];                   //remove entry @ randomindex list can't re-used.                 gridpositions.removeat (randomindex);                   //return randomly selected vector3 position.                 return randomposition;              }               //layoutobjectatrandom accepts array of game objects choose along minimum , maximum range number of objects create.             void layoutobjectatrandom (gameobject[] tilearray, int minimum, int maximum)             {                 //choose random number of objects instantiate within minimum , maximum limits                 int objectcount = random.range (minimum, maximum+1);                  //instantiate objects until randomly chosen limit objectcount reached                 for(int = 0; < objectcount; i++)                 {                      //choose position randomposition getting random position our list of available vector3s stored in gridposition                     vector3 randomposition = randomposition();                         //choose random tile tilearray , assign tilechoice                     gameobject tilechoice = tilearray[random.range (0, tilearray.length)];                       //instantiate tilechoice @ position returned randomposition no change in rotation                     instantiate(tilechoice, randomposition, quaternion.identity);                  }              }               //setupscene initializes our level , calls previous functions lay out game board             public void setupscene (int level)             {                  //reset our list of gridpositions.                 initialiselist ();                  //instantiate random number of objects based on minimum , maximum, @ randomized positions.                 layoutobjectatrandom (randomobject1, random1count.minimum, random1count.maximum);                 layoutobjectatrandom (randomobject2, random2count.minimum, random2count.maximum);                 layoutobjectatrandom (randomobject3, random3count.minimum, random3count.maximum);                 layoutobjectatrandom (randomobject4, random4count.minimum, random4count.maximum);               }         }     } 

when instantiate final gameobject, you're not giving position. understand want position in same place last randomly positioned gameobject, offset in x axis. you'll need vector3 contains position of last randomly spawned gameobject, i'll call lastrandompos. assume know how value, if not leave comment , i'll include in answer. can pass position when instantiating final gameobject:

var objpos = lastrandompos; objpos.x = objpos.x + <however want offset in x>; instantiate(obj, objpos, quaternion.identity); 

edit: easiest way position of last random object set class-level variable every time instantiate object in layoutobjectatrandom. declare in class scope, example underneath declaration of gridpositions:

private list <vector3> gridpositions = new list <vector3> ();   //a list of possible locations place objects. private vector3 lastrandompos; 

then when instantiate random object can set variable keep track of spawned:

instantiate(tilechoice, randomposition, quaternion.identity); lastrandompos = randomposition; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -