- Home /
Spawning objects around a point
Hello! I am somewhat new to scripting in Unity and have no idea of ways to do this. I want to spawn a cube made of smaller cubes around 0,0,0. However, the size of the big cube spawned will differ based on the number of little cubes used building it, but still needed to remain centered at 0,0,0. Is their any way to move a group of objects to center them at 0,0,0 or is their another way to do this. Thanks.
I do know how to spawn objects, I just don't know how to spawn a bunch of them around a point
I don't really understand but you define randomly on start the size of the cubes (the littles) which is the same for all right ? Then you want define the number of cube (little) on a side and then you build it like that ?
OR
You define the size of a side, then you divide it by the size of a little cube (size of little is still choose randomly) ?
It's just to know how to explain how to do that :)
And I think this is something that can really help you here : http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html
About moving a group of objects, try to make the other objects a child of an object, that way, when you try to move the parent object, all of them will follow.
so you are saying break the big cube of the size chosen into a bunch of little cubes? Is there even a way to do that?
hmm, you could create a small cube, duplicate it and use them to create a bigger one. as you duplicate that small cube, make the duplicated cube a child of the original, that way, when you move the original cube, every cube will follows.
Answer by chesterr400 · Oct 25, 2012 at 06:34 AM
Hi!
First create that "cube made of smaller cubes" and put it into a prefab.
Next, copy the code below (this is a code I found on unity answers too and just modified it, credits to the owner of the code and I forgot who he/she is) and paste it into a new JS script.
Then, create an empty game object and drag the JS script in it.
Select the prefab that you've created and add a rigidbody on it.
Once the prefab has a rigidbody, click the empty game object with the script, apply the prefab that you've created to that script.
Make sure that the "cube made of smaller cubes" you've made is positioned at (0,0,0)
var timer : float = 0.0;
var spawning : boolean = false;
var prefab : Rigidbody;
var spawn1 : Transform;
function Update () {
//check if spawning at the moment, if not add to timer
Debug.Log(Application.loadedLevelName);
if(!spawning){
timer += Time.deltaTime;
}
//when timer reaches 2 seconds, call Spawn function
if(timer >= 2){
Spawn();
}
}
function Spawn(){
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPick : int = Mathf.Abs(Random.Range(1,3));
//create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
var location : Transform;
var thingToMake : Rigidbody;
//check what randomPick is, and select one of the 3 locations, based on that number
if(randomPick == 1){
location = spawn1;
Debug.Log("Chose pos 1");
thingToMake = Instantiate(prefab, location.position, location.rotation);
}
//halt script for 1 second before returning to the start of the process
yield WaitForSeconds(1);
//set spawning back to false so timer may start again
spawning = false;
}
I don't think it's what he needs or wants...
And I don't think give him a solution like that is the good thing to do
I'm sorry, thanks for the criticism, I'll try to make a better way of answering ^_^
Your answer
Follow this Question
Related Questions
Custom level creator for players 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer