- Home /
Random spawn points for my target prefabs?
I want the FPS mini game i'm making on Unity replayable, is there any way I can make my target prefabs spawn into random areas of the game?
Answer by fafase · Oct 29, 2012 at 10:26 AM
GameObject obj = (GameObject)Instantiate(prefab,new Vector3(Random.Range(a,b),Random.Range(a,b),Random.Range(a,b)),Quaternion.identity);
Now you need to give values to a's and b's so that object does not get instantiated outside the boundaries of your game.
Edit: Instead of totally random, you could have an array of position that you randomly choose from. For instance, add an empty object to your scene, tag it as SpawnPoint. Create a prefab and drag and drop the empty object into it. Remove the original one from the scene.
Now from your new prefab start placing them in the scene where you would like your objects to be created. Place them high enough so that your newly instantiated object is not halfway through which would make it fall down below the terrain. You could also place them behind a wall or any environment thing that would hide the instantiation.
Now, in your object to be created add a script like this:
Vector3[] = vecArray;
int top;
void Start(){
GameObject objs = GameObject.FindGameObjectsWithTag("SpawnPoint");
foreach(GameObject obj in bjs){
vecArray = objs.GetComponent<Transform>().position;
}
top = vecArray.Length;
InvokeRepeating("CreateObject",0.01f,10.0f);
}
void CreateObject(){
Instantiate(prefab,vecArray[Random.Range(0,top)],Quaternion.identity);
}
Now I see coming the "I don't use C#" So same is:
var vecArray:Vector3[];
var top:int;
function Start(){
var objs = GameObject.FindGameObjectsWithTag("SpawnPoint");
for (var obj in objs){
vecArray = obj.GetComponent(Transform).position;
}
top = vecArray.Length;
InvokerRepeating("CreateObject",0,01f,10.0f)
}
function CreateObject(){
Instantiate(prefab,vecArray[Random.Range(0,top)],Quaternion.identity);
}
It will take generate a random value for the index of the array and will get the Vector3 stored there. InvokeRepeating will call the CreateObject function from the start of the scene and every 10s will spawn a new object.
I was trying to use your C# code for my own project, and I ran into this error: error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for
GetComponent' and no extension method GetComponent' of type
UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
It doesn't like the GetComponent bit in the array assignment.
I think there is a problem in c# code. In the 5th line "
foreach(GameObject obj in bjs){
" there is bjs ins$$anonymous$$d of objs. Am I right?
Answer by shiranui93 · Oct 29, 2012 at 10:50 AM
Will I also need to put a y value as I don't want the target to go to high so the player can't hit it. Also is that Javascript?
Your answer
Follow this Question
Related Questions
Rigidbody bullets won't spawn at gun barrel... 2 Answers
collider doesnt react to script 0 Answers
Vuforia, how to spawn a prefab on target image and not above? 1 Answer
Frame rate drops when replaying level 0 Answers
Troubles With A Shoot Script 1 Answer