Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

script to randomly spawn objects in given area

Discussion in 'Scripting' started by janiashvili, Feb 24, 2012.

Thread Status:
Not open for further replies.
  1. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    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
     
    KonniosGames likes this.
  2. Posly

    Posly

    Joined:
    Jan 14, 2012
    Posts:
    151
    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")
     
    shekalo and VraunMM like this.
  3. markusb

    markusb

    Joined:
    Mar 20, 2010
    Posts:
    157
  4. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    Cheers,

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

    deps

    Joined:
    Nov 19, 2009
    Posts:
    78
    Invoke("MyShipSpawningFunction", Random.Range( 0.5, 2.9 ) );
     
  6. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    OK.

    Code (csharp):
    1. function Update () {
    2.  
    3.         Invoke("Enemy1Spawn", Random.Range(0.5, 2.9) );
    4.        
    5.    
    6. }
    7.  
    8.  
    9. function Enemy1Spawn () {
    10.     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 :D
     
    Last edited: Feb 25, 2012
  7. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    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)
     
  8. deps

    deps

    Joined:
    Nov 19, 2009
    Posts:
    78
    Code (csharp):
    1.  
    2. function Start() // Or Awake, but I prefer to use Start
    3. {
    4.    Invoke("SpawnEnemy", Random.Range(1,3)); // Spawn an enemy
    5.    // You could call SpawnEnemy(); direct here if you want to spawn one right away
    6. }
    7.  
    8. function SpawnEnemy()
    9. {
    10.    // Spawn enemy/enemies here...
    11.    // Just do a for loop if you want to spawn a group of them
    12.  
    13.    Invoke("SpawnEnemy", Random.Range(1,3)); // Spawn new enemy in a random amount of time
    14.  
    15. }
    16.  
     
  9. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    here is what i'm getting with it http://imageshack.us/photo/my-images/545/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
     
    Last edited: Feb 25, 2012
  10. deps

    deps

    Joined:
    Nov 19, 2009
    Posts:
    78
    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:
    Code (csharp):
    1.  
    2. function Start() {
    3. SpawnEnemy();
    4. }
    5.  
    or you could do this to spawn an enemy after a while:
    Code (csharp):
    1.  
    2. function Start() {
    3. Invoke("SpawnEnemy", Random.Range(1,3));
    4. }
    5.  
    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:
    Code (csharp):
    1.  
    2. function SpawnEnemy()
    3. {
    4.    for( var i = 1; i < 5; i++ )
    5.    {
    6.       // Call instantiate here
    7.    }
    8. }
    9.  
    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.
     
  11. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    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 :D)
     
  12. janiashvili

    janiashvili

    Joined:
    Feb 24, 2012
    Posts:
    66
    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)
     
  13. VraunMM

    VraunMM

    Joined:
    Apr 21, 2016
    Posts:
    1
    Thank You
     
  14. momobobobomo

    momobobobomo

    Joined:
    Jun 13, 2023
    Posts:
    2
    Maybe if you weren't such a dbag asking for help, you wouldve gotten help
     
  15. UnderProfile

    UnderProfile

    Joined:
    Mar 17, 2024
    Posts:
    3
    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);
    }
     
  16. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,520
    Eliminate the x and put a zero.
    Code (CSharp):
    1.  
    2.     void Make()
    3.     {
    4.        Vector3 v = new Vector3(0f, Random.Range(2.41f, 6.82f), Random.Range(-4.6f, 5f));
    5.        Instantiate(go, v, Quaternion.identity);
    6.     }
    7.  
    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().

    Code (CSharp):
    1.  
    2.     void MakeImproved(Vector3 spawnPt, float range)
    3.     {
    4.        Vector3 v = spawnPt + new Vector3(0f, Random.Range(-range, range), Random.Range(-range, range));
    5.        // most people's games don't often have situations where x is zero tho, not sure what's diff in your case
    6.        Instantiate(prefab, v, Quaternion.identity);
    7.     }
    8.  
     
    MelvMay likes this.
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,576
    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.
     
Thread Status:
Not open for further replies.