- Home /
Is it possible to instantiate a prefab in 2d game from particular locations without using empty game object?
I am working on a 2d game i i want to spawn different prefabs randomly from random positions. i am using empty game objects at different positions and using random function to generate enemies from different positions.. what i need is i want to spawn enemies without using empty game objects from different locations. how can i use those locations in array and in my random method ? hope so you got what i really want to know.
Answer by Yuvii · Oct 10, 2017 at 02:36 PM
Hey,
Not sure if i get it, but i'll try.
In the unity doc you can see that you can instantiate like that
Instantiate(Object original, Vector3 position, Quaternion rotation);
It doesn't realy need an empty game object, but a vector 3. So you can try something like
Instantiate(*your object*, new Vector3 ( Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ)), *your quaternion*);
Well in a 2D game you only need 2 axes, but you get the idea :-)
yes i am clear enough . one thing that i am not getting is how to put my positions in n array ? for example i have 8 positions from where i want to instantiate objects randomly.. so how can i put positions in array and generate randon positions here new Vector3 ( Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY), Random.Range($$anonymous$$Z, maxZ)) ???????
Just declare a public Vector2[] myPositions;
variable in your monobehaviour and you can insert as many Vector2s in it as you like in the inspector.
then choose a random one from them with Vector2 selectedPosition = myPositions[Random.Range(0, myPositions.Length)]
in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat
@Yuvii in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat
@Yuvii in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a random location and use that location to instantiate prefab.
Hey sorry i just saw your comment.
Well to do so, what i'd do myself is keep the empty gameobjects system and then add a random vector3 as an offset to it.
So something like that :
public Transform[] differentSpawners;
Vector2 offset = new Vector2(Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY));
Instantiate(*your object*, differentSpawners[Random.Range(0, differentSpawners.Lenght)].position + offset, *your quaternion*);
The advantage of keeping the Empty GO system is that you can easily move them in the inspector.
Hope that helped :)
Answer by marcrem · Oct 10, 2017 at 03:26 PM
Yuvii's answer is pretty much my solution. However if you wanted to specify locations to randomly choose from, then just put some Vector3s in an array!
yes i am clear enough . one thing that i am not getting is how to put my positions in n array ? for example i have 8 positions from where i want to instantiate objects randomly.. so how can i put positions in array and generate randon positions here new Vector3 ( Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY), Random.Range($$anonymous$$Z, maxZ)) ???????
in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat
I'll go with how I'd do it, although I might not be using the best way.
I'd do a List.
so at the top of your code you declare a list : List positionsList;
then in your start function: positionsList = new List();
Then, after that, you can add as many vectors as you'd like to the list, by doing: positionsList.Add(new Vector3(x,y,z));
So now, cool! You've added vectors to the list. Now how do you randomly pick one?
at the top of your code, delare an int: int pickedPosition;
then, whereever you want to randomly choose one of the positions in your vector list, you would do this:
pickedPosition = Random.Range(0, (positionsList.Count - 1)); // choose between 0 (the first vector in your list) and the last one, meaning the total count of your list $$anonymous$$us one because 0 counts for one. Instantiate(yourObject, positionsList[pickedPosition], Quaternion rotation); //Here it's going to spawn your object at the previously randomly picked item on your list.
Hope that helps!
in the instantiate line, you might wanna change "Quaternion rotation" to say.. Quaternion.identity