Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Propability of a Cube having a specific Component

Discussion in 'Scripting' started by Snickbrack, Oct 13, 2022.

  1. Snickbrack

    Snickbrack

    Joined:
    Dec 29, 2020
    Posts:
    5
    Hi,

    I want to create a functionality with which I am able to set-up propabilities for a set of Components to be applied to an GameObject.

    Lets say I have these Components:

    - SpeedUpgrade
    - HealthUpgrade
    - StealthUpgrade

    And these have all a Chance of 33% of being selected for the GameObject. So there should be a random selection between them based on the specified chance (it could also be 20/20/60 %)

    How can do I this?
     
  2. Snickbrack

    Snickbrack

    Joined:
    Dec 29, 2020
    Posts:
    5
  3. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    So theres a 33% chance of 1 of the 3 to be selected, or 33% chance of each to be selected
    This thread should help you out
    https://forum.unity.com/threads/ran...bject-having-a-chance-of-being-picked.485792/
     
    Snickbrack likes this.
  4. Snickbrack

    Snickbrack

    Joined:
    Dec 29, 2020
    Posts:
    5
    @Johan_Liebert123 Yeah but it should pick one and only one.

    So I would go for this one of your option "So theres a 33% chance of 1 of the 3 to be selected"
     
    Johan_Liebert123 likes this.
  5. Snickbrack

    Snickbrack

    Joined:
    Dec 29, 2020
    Posts:
    5
    Would it be an option to say there is a range from 0 to 1 and apply an specific part of this range to the components to be applied?

    So for my example this would be:

    - SpeedUpgrade (0-0,33)
    - HealthUpgrade (0,33-0,66)
    - StealthUpgrade(0,66-1,0)

    Or if we change the chances:

    - SpeedUpgrade (20%) (0-0,2)
    - HealthUpgrade (20%) (0,2-0,4)
    - StealthUpgrade (60%) (0,4-1,0)

    Is this something similar to a chance calculation and will this be something equal to the desired chances?
     
  6. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    I guess you can do something like this

    Code (CSharp):
    1.         int number = Random.Range(0, 100);
    2.         if(number <= 33)
    3.         {
    4.             manager.SpeedUpgrade;
    5.         }
    6.         else if(number > 33 && number <= 66)
    7.         {
    8.             manager.HealthUpgrade;
    9.         }
    10.         else if(number > 66)
    11.         {
    12.             manager.StealthUpgrade;
    13.         }
    Try that out,
    manager.SpeedUpgrade
    etc is the method you wanna call once its been selected
    EDIT: You can create a simplified version of that, but c# and maths are not my thing and you can also change the chances based on your needs
     
    Snickbrack likes this.
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    A simple way is to roll a random # from 1-100, then use IF statements to decide on which version. Something like:
    Code (CSharp):
    1. int spawnTypeRoll = (code for random 1 to 100 int)
    2.  
    3. // odds here are for 20/20/50/10:
    4. if(spawnTypeRoll<=20) spawnType=1;
    5. else if(spawnTypeRoll<=40) spawnType=2;
    6. else if(spawnTypeRoll<=90) spawnType=3;
    7. else spawnType=4;
    There are slicker ways, but this way is easy to understand as far as what the code does. I just set spawnType, but you could have it Instantiate type 1, 2, 3 or 4 items instead.
     
    Snickbrack and Johan_Liebert123 like this.
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    If you used only IF's, you'd need to list both ends, like "between 34 and 66", but a neat thing about if/else (which is what you have) is you can list only the ending value. 1-33 is taken care of by the 1st IF. If we get to the second, after the else, we know the number is 34 or more, so don't need to check. Checking just <=66 is automatically 34-66. It's a common trick with cascading IF's like that.

    That's a little less error-prone (no risk we'll cover 1-33 and 35-66 and miss 34), but mostly it's easier to read and change. Each breakpoint is listed just once -- one 33, one 66.
     
    Snickbrack and Johan_Liebert123 like this.
  9. Snickbrack

    Snickbrack

    Joined:
    Dec 29, 2020
    Posts:
    5
    Okay understood, thanks!
     
  10. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Oh, never knew that. Thats pretty handy, learning new stuff every day now from you all, thanks a lot!
     
    Snickbrack likes this.