script to randomly spawn objects in given area

Greetings,

I want some kind of script to randomly spawn objects(in this scenario enemy ships), this game should be for “play as long as you can” facing enemy ships coming randomly from given area(e.g. randomly from top left).

AI(if you can call it so) for enemy ships are very simple - just come from top to bottom of screen(and some from bottom to top), and shoot lasers if they can.

So, I want to make those object randomly spawnable at given range. and of course, with random intervals from previous to next one. And, even better will be if those spawning rate will rise(e.g. 10 per minute at beginning and 100 per minute after 5 of play), even better would be if they’ll spawn in groups(e.g. 2 now, then 5, then 3, randomly) and amount of groups to arise would be great.

Please, tell me how to make it in javascript

1 Like

Random.Range picks a random number in between two numbers you choose, so of you wanted to instantiate something it would look like:

Instantiate(“the ship you want”, Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max)), “rotation”)

2 Likes

Unity got a function for stuff like this:
http://unity3d.com/support/documentation/ScriptReference/Random-insideUnitCircle.html
and the same in 3d:
http://unity3d.com/support/documentation/ScriptReference/Random-insideUnitSphere.html

2 Likes

Cheers,

and how to randomly spawn them(randomly in a time)?

Invoke(“MyShipSpawningFunction”, Random.Range( 0.5, 2.9 ) );

OK.

function Update () {

		Invoke("Enemy1Spawn", Random.Range(0.5, 2.9) );
		
	
}


function Enemy1Spawn () {
	Instantiate(enemies[1], Vector3(Random.Range(-4.5, 4.5), 0.3, 5), transform.rotation);

but it spawns hundreds of them per spawn … and not only 1 :smile:

I moved Invoke from there to function Awake.

but, unfortunately, with no big success - it spawns one object. or, if it will be “InvokeRepeating” it will spawn objects and stack them in one group(just randomly spawn them in one group). So they all move together(not randomly spawn. but they randomly spawn in one group rather than randomly spawning at all)

function Start() // Or Awake, but I prefer to use Start
{
   Invoke("SpawnEnemy", Random.Range(1,3)); // Spawn an enemy
   // You could call SpawnEnemy(); direct here if you want to spawn one right away
}

function SpawnEnemy()
{
   // Spawn enemy/enemies here... 
   // Just do a for loop if you want to spawn a group of them

   Invoke("SpawnEnemy", Random.Range(1,3)); // Spawn new enemy in a random amount of time

}

here is what i’m getting with it ImageShack - 34387496.png
(all the objects spawning in one kind of group)

or I just misunderstood you.

// You could call SpawnEnemy(); direct here if you want to spawn one right away
you meant to write Invoke(“SpawnEnemy”, Random.Range(1,3)); right there? or what?..

// Spawn enemy/enemies here…
all the enemies what should be spawn(e.g. enemy[1], enemy[2]) right?

// Just do a for loop if you want to spawn a group of them
what?..

and why did you wrote invoke in SpawnEnemy function?.. I didn’t get

If you use Invoke, it will call the function you specify in the first argument after the delay you specified as the second argument.
You can call the function directly instead if you want to spawn an enemy right away.
Just to clarify; you could do this to spawn an enemy right away:

function Start() {
SpawnEnemy();
}

or you could do this to spawn an enemy after a while:

function Start() {
Invoke("SpawnEnemy", Random.Range(1,3));
}

Depends if you want to spawn one or more.

If you want to spawn more than one enemy, for example a group of enemies, every time SpawnEnemy is called you can use a for-loop:

function SpawnEnemy()
{
   for( var i = 1; i < 5; i++ )
   {
      // Call instantiate here
   }
}

That’s because one can do that to make unity call the function again in a new random interval.
You mentioned InvokeRepeating but it will use the same delay between the intervals, but if you manually invoke the function from inside itself you can specify how long it should wait. In my case I picked a new random interval.

thank you, but take a look at image I showed you.

you see - it spawns only one group and many things inside it.

I ask for many single objects (not grouped like that). you know to spawn randomly like it does, but also to spawn them in initial position of that range(not when that range moves :smile:)

hello!

You can’t help me?

I just want to spawn objects at random time(with no waves or anything similar; just at random time)

Thank You

Maybe if you weren’t such a dbag asking for help, you wouldve gotten help

I know this is after a loong time, though i am facing the same issue, though it just isnt working. I am using Random. Range, though i dont want to change the x position, though i do want to do the y and z so is this how i do it?
GameObject hit;
GameObject res;

void Start()
{
hit = GameObject.FindWithTag(“hit”);
res = GameObject.FindWithTag(“Respawn”);

}

void Update()
{
Invoke(“Make”, 0.1f * Time.deltaTime);
}

void Make()
{
Instantiate(hit, new Vector3(Random.Range(-3.67f, -3.68f), Random.Range(2.41f, 6.82f), Random.Range(-4.6f, 5f)), Quaternion.identity);
}

Eliminate the x and put a zero.

    void Make()
    {
       Vector3 v = new Vector3(0f, Random.Range(2.41f, 6.82f), Random.Range(-4.6f, 5f));
       Instantiate(go, v, Quaternion.identity);
    }

But a general lesson: rather than having magic numbers, you often want to spawn within a random range of a spawn point, which is just a vector3 often pulled from a gameobject/transform you have in your scene that exists just for marking where things will spawn. This point and its range can be made visible with OnDrawGizmos() or OnDrawGizmosSelected().

    void MakeImproved(Vector3 spawnPt, float range)
    {
       Vector3 v = spawnPt + new Vector3(0f, Random.Range(-range, range), Random.Range(-range, range));
       // most people's games don't often have situations where x is zero tho, not sure what's diff in your case
       Instantiate(prefab, v, Quaternion.identity);
    }
1 Like

If you have an issue then please create your own thread rather than necroing an old (2012!) thread. Also, please use code-tags when posting code.

Thread closed.