- Home /
Need instantiated objects to spawn at transform NOT at origin?(Solved)
I was instantiating objects either side of my spawner on the x axis. Which worked fine, just enter how many units +/- along the X axis and they would happily appear.
Now we jump to my 2nd scene and I couldn't figure out why the spawned objects were nowhere to be seen. After some head scratching I realised they were spawning 'inside' the terrain. Eventually I've realised that in scene 1 the spawner was 0,0,0 origin and the objects spawned xxx either side.
However in scene 2 my spawner is about 80 units further along the x axis but my instantiated objects are still spawning either side of origin.
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, transform.position.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
I'm not 100% sure that's what was happening but its the only thing that makes sense as to why the objects are spawning out of position. So how do I make it so still spawn withing the ranges I set as 'spawnvalues' BUT make that value based on the transforms actual position.
???
Answer by tanoshimi · Mar 21, 2015 at 08:15 PM
Line 8 should be:
Vector3 spawnPosition = transform.position + new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, 0);
Answer by SebastianLague · Mar 21, 2015 at 10:35 PM
You should add the spawner's current position to spawnPosition:
Vector3 spawnPosition = transform.position + new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y);
Your answer
![](https://koobas.hobune.stream/wayback/20220613190847im_/https://answers.unity.com/themes/thub/images/avi.jpg)