- Home /
Spawn position is different
Im making a game for android where when you tap on the screen, the gravity changes and you can walk on the ceiling. Just like gravity guy! But what is different in this game is that its random generated. So i made some obstacles of cubes and put them in an empty game object. I then put it in a prefab and dragged it into my spawn-script. I have 4 different obstacles. The spawn script is attached to a cube which spawns the obstacles about every 7 sec. And this is where the problem appears.
When these obstacles spawns its a different position on thex axis they spawn. Little hard to explain, but i have some pictures that can explain some.
First picture: https://www.dropbox.com/s/lp5trrh7zibtdpk/spawnposition.tiff
Second picture: https://www.dropbox.com/s/rfublvsmnx84pct/spawnposition2.tiff
These are two obstacles i have in my game. The cube that is on both pictures somewhere between the two obstacle pictures, is the spawner. I took the picture right after they spawned. As you can see in the last picture, the obstacle spawned, where the spawner was in the middle. But in the first picture, the spawner is on the left side. And that irritates me a lot. it also happens with some of the other obstacles. How can i fix this? If you have any questions, plz ask.
Spawner script:
#pragma strict
//Instantiate(asteroid, myposition.transform.position, myposition.transform.rotation);
var rndNum:int;
var course1 : Transform;
var course2 : Transform;
var course3 : Transform;
var course4 : Transform;
var course5 : Transform;
var course6 : Transform;
var myposition : GameObject;
var spawn = true;
function Start() {
while(true) {
var rndNum:int = Random.Range(1 , 5);
Debug.Log(rndNum);
if(rndNum == 1)
{
transform.position.y = -2.100585;
Instantiate(course1, myposition.transform.position, myposition.transform.rotation);
}
if(rndNum == 2)
{
transform.position.y = 1.195426;
Instantiate(course2, myposition.transform.position, myposition.transform.rotation);
}
if(rndNum == 3)
{
transform.position.y = 1;
Instantiate(course3, myposition.transform.position, myposition.transform.rotation);
}
if(rndNum == 4)
{
transform.position.y = 0.2;
Instantiate(course4, myposition.transform.position, myposition.transform.rotation);
}
if(rndNum == 5)
{
Instantiate(course5, myposition.transform.position, myposition.transform.rotation);
}
if(rndNum == 6)
{
Instantiate(course6, myposition.transform.position, myposition.transform.rotation);
}
yield WaitForSeconds(22);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613134142im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
spawn muzzle flash with random rotation? 2 Answers
Random position without overlap. HELP! 1 Answer
How can I calculate and place a GameObject at a random place just outside the camera frustrum? 0 Answers
How do I randomly place doors, treasure chests, and traps in a randomly generated maze? 0 Answers
Random position inside multiple areas 2 Answers