Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random Shuffle of GO Array Considering Ratios

Discussion in 'Scripting' started by groovfruit, Nov 23, 2010.

  1. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Hi

    As a very novice 'programmer', I've been researching random behaviors to instantiate game objects.

    I've read through forum posts and example code, including Andee's Randomness, including Shuffle, through to Enemy Spawn.

    Here's my conundrum that is doing my head in.

    I know how to spawn objects randomly. However, what i need to achieve is the following:


    1. I have 9 game objects (prefabs) that i need to spawn, each with it's own attributes
    2. I want to be able to randomly spawn from the above 9, with this spawn rate increasing over time
    3. I'd like to determine the ratio that the above are spawned. I.e. GO1 is common enemy and GO2 is a rare enemy. Therefore I'd like for every 20 GO1 a GO2 to spawn (ratio 20:1) and so forth. I'd like to also increment/increase this ratio over time, such that the GO2 enemy becomes more apparent as time goes on (to increase level of difficulty)


    Any advice on direction to do this would be greatly appreciated. Somehow I think i've looked at so many examples and scripts that my mind is a scramble! I can't see the tree through the woods.

    Cheers
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Break the problem down and solve each sub-problem separately. You need a way to represent the changing master spawn rate, and track when you last spawned an ememy / when you need to spawn a new one. You need a way to actually spawn an enemy when it's time to do so. You need a way to represent the relative frequencies/probabilities of each spawn type, and a way to adjust them over time. You need a way to determine which enemy to spawn based on those frequencies/probabilities. And, once you have a way to represent the information that's going to vary over time, you need to determine *how* you want it to vary -- i.e. how rapidly the spawn rate increases, how the spawn frequencies of each enemy type should be balanced as they change, etc.

    For example: Start by getting one enemy to spawn every N seconds. Then work on making N vary/decrease over time. Then expand that to multiple enemy types, with an equal chance of spawning any of them each time. Then add bias to make some more likely to spawn / spawn more frequently. Once you have that you should have everything in place to start refining the mechanics of how the spawn frequencies change.

    Breaking it down into simpler pieces like that should allow you to get some code started, and post specific questions if/when you get stuck.
     
  3. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Thanks Laurie this is excellent advice. I think one of the limitations to 'working solo' is not having someone else to see the problem in a different light for you and being a sounding board
     
  4. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Very true, which is what makes the forums such a great resource ;-)
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can do this using my Randomness library - the function you need is Sample. To use it, create an array of floats which contain the ratios of the different enemies. So, if the proportions are 20:4:1, use code like this:-
    Code (csharp):
    1. var spawnRate: float[];
    2.  
    3. function Start() {
    4.   spawnRate[0] = 20;
    5.   spawnRate[1] = 4;
    6.   spawnRate[2] = 1;
    7. }
    8.  
    9.   ...
    10. var enemyIndex = Sample(spawnRate);
    With this version of the Sample function, you can just change the values in the spawnRate array to change the probability of the corresponding enemy getting chosen.
     
  6. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Hi Andee... thanks again for your advice... I see it everywhere through the forums :)

    I'll give this a shot and let you know how it goes!