- Home /
How to make an object spawn in different positions?
I have a script that spawn my cube in the same place i want it to spawn at different positions every time ? how do i do this ? Thanks in Advance!!
var prefabToSpawn:Transform;
var spawnTime:float;
var spawnTimeRandom:float;
private var spawnTimer:float;
function Awake ()
{
ResetSpawnTimer();
}
function Update()
{
if(spawnTimer > 0)
{
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0.0)
{
spawnTimer = 0;
Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
ResetSpawnTimer();
}
}
}
function ResetSpawnTimer()
{
spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
}
Answer by Sjiggle · Jul 04, 2013 at 11:36 PM
If you want randomness, try this:
Instantiate(prefabToSpawn, transform.position + Vector3(Random.Range(0f, 10000f),Random.Range(0f, 10000f), Random.Range(0f, 10000f)), Quaternion.identity);
this essentially adds a random Vector3 to the original transform position of the gameobject you attach this script to. For a more accurate random (yet pregiven) spawn, use an array of spawn points (vector3s) and do get a random index from those spawnpoints to spawn on to.
Why using transform.position + Vector3? Your range is quite big enough. By the way it is too big. Using those values will throw a warning. But the idea is there.
Also, you may want to check if the position is valid, what if your spawn is underground? Best is not totally random by using an array of predefined points.
The question states: I have a script that spawn my cube in the same place i want it to spawn at different positions every time ? how do i do this ? Thanks in Advance!!
He wants to spawn at different points. Think of it as pseudo code.
Answer by trs9556 · Jul 04, 2013 at 11:27 PM
Line 26 reads as followed:
Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
The part that says transform.position is the location where prefabToSpawn will spawn. Change this value and you'll get a different location.
You can do something along the lines of what you did in your ResetSpawnTimer() method.
Create a random range to "offset" the position.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
If you need further help let me know.
EDIT
var prefabToSpawn:Transform;
var spawnTime:float;
var spawnTimeRandom:float;
var minimumXValue : float;
var maximumXValue : float;
var minimumYValue : float;
var maximumYValue : float;
var minimumZValue : float;
var maximumZValue : float;
private var spawnTimer:float;
function Awake ()
{
ResetSpawnTimer();
}
function Update()
{
if(spawnTimer > 0)
{
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0.0)
{
spawnTimer = 0;
var offset : Vector3; //a vector3 is (x, y, z)
offset = new Vector3(Random.Range(minimumXValue, maximumXValue), Random.Range(minimumYValue,maximumYValue), Random.Range(minimumZValue,maximumZValue));
Instantiate(prefabToSpawn, transform.position + offset , Quaternion.identity);
ResetSpawnTimer();
}
}
}
function ResetSpawnTimer()
{
spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
}
thank you very much i think i can do it myself if not is it ok if i contact you ? Thanks Again!
By all means you are more than welcome to contact me if you need help, and or post it in the comments.
What @Sjiggle just posted was just about exactly the same thing I was going to. Except ins$$anonymous$$d of the range being (0, 10000) I was going to make vars that you can set in the inspector that way you have control over the area it spawns.
Any chance one of you will do it for me I am really under pressure and i dont have the time thank you !
bump i have tried it myself, it hasn't worked to well for me how do I set it so it spawns in a certain area ?
Your answer
Follow this Question
Related Questions
Spawning Cube on empty gameobject 1 Answer
Random cube length on instantiate... 1 Answer
Respawn after falling off script? 2 Answers
Spawning in Specific Areas 1 Answer
How can I spawn another game object once one has been destroyed? 2 Answers