- Home /
Generate an object in random positions ?
Hello,
I tried to generate an object in random positions but it didn't work good, it was generating very fast, i didn't know why? also the rotation is generating in weird way, I wanted all the rotation as the original object, I used this code:
#pragma strict
var theObject:Transform;
var theNewPos:Vector3;
var maxPos:int=2000;
var minPos:int=9;
var max=1;
var i = 0;
function Start(){
}
function Update(){
for(i=0;i<max;i++)
{
spawn();
}
}
function spawn() {
yield(WaitForSeconds(2));
theNewPos= new Vector3 (Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
Instantiate(theObject,theNewPos,transform.rotation);
}
Thanks.
Answer by robertbu · Feb 28, 2013 at 10:41 AM
Here is a rewrite of your code. I made a lot of changes which I usually don't like to do.
var theObject:GameObject;
var maxPos:float = 2000.0;
var minPos:float = 9.0;
var max = 22;
function Start(){
StartCoroutine(spawn());
}
function spawn() : IEnumerator {
for (var i = 0; i < max; i++) {
yield WaitForSeconds(2.0);
var theNewPos= new Vector3 (Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
var go : GameObject = Instantiate(theObject);
go.transform.position = theNewPos;
}
}
A few comments:
Positions and positions in Unity are float values. While int variables can be converted, there are some bugs that pop out when using ints instead of floats.
If you call Instantiate() without parameters, it uses the position and rotation of the original object. Then on the next line I set the position, but left the rotation alone.
Your method of trying to spawn one every 2 seconds would not work. You could have done a timer implemented in Update() rather than the solution I have here.
Thanks alot, I don't know all unity commands and functions, I programmed alot before but not in unity, would suggest me some good sites that explains the functions? thanks.
@robertbu, do you $$anonymous$$d explaining why the yield()
in his script will not work. Does it have something to do with the StartCoroutine()
?
I think this is how it works. I don't have as strong a foundation with the IEnumerator/yield as I would like:
Let's assume the app was running at 60 fps and we were getting 60 Update() calls per second as well. The original code was calling spawn() once per frame (max was == 1). Each call generated a suspended version of the spawn(), so there were 120 suspend method calls in 2 seconds. At the end of 2 seconds the first one fired, and at 2 + 1/60 the second one would fire, and at 2 + 2/60 the third one would fire. So the code had a 2 second pause and the a whole slew of game object would be generated at the rate of one per frame. Of course, the code was continuing to call spawn at the rate of once per frame keeping 120 calls stacked up and waiting to go.
@Saif $$anonymous$$ohamd: @Julien.Lynge includes a list of resources in many posts. I've spent time with some of them.
Take a look at this answer:
http://answers.unity3d.com/questions/378740/where-can-i-begin-to-learn-how-to-make-an-inventor.html
@robertbu, Hey guys i am new in unity script.Robertbu thanks for your script.its working fine. But how can i create those clone object in random position. Actually someone wants to test my unity skills. So they offer me a project. Project description is like this:- (1) Create a ground (2) generate some Sphere (Sphere are randomly pop out from ground or some other game objects in random position/time and move toward the sky and destroy automatically after few seconds) . Before they destroy automatically have to shoot them. Now i can create the shooter and bullets. But i am struggling with the random Sphere creation procces.Please help me.