Search Unity

Random Numbers and Chance

Discussion in 'Scripting' started by TheChronicPhenix, Mar 7, 2011.

  1. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Ok so I have a character that has three attacks, two normal attacks and a special attack, what I want to do is have it randomly choose between the three attacks but have a much lower chance of getting the special attack. I know how to do the random number, but how would I go about doing the chance part? Thanks.
     
    Raisito likes this.
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    for example:
    generate a random number from 0 to 1. If it is < 0.45 normal attack 1, if it is >= 0.45 and < 0.9 normal attack 2. If it is >= .9 special attack
     
    ammarajam08 and Raisito like this.
  3. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    You can get a random value between 0 and 1 and use different intervals of that value to decide between the different attacks.

    C# example:

    Code (csharp):
    1. float randValue = Random.value;
    2. if (randValue < .45f) // 45% of the time
    3. {
    4.     // Do Normal Attack 1
    5. }
    6. else if (randValue < .9f) // 45% of the time
    7. {
    8.     // Do Normal Attack 2
    9. }
    10. else // 10% of the time
    11. {
    12.     // Do Special Attack
    13. }
    Edit: Not only did you beat me to it, but you even used the same values as I did. :)
     
    ammarajam08, Raisito, blisz and 2 others like this.
  4. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    great minds think alike :)
     
    Raisito and unity_VmcpUflwpP1mUw like this.
  5. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Thanks a lot works perfect! Can't believe I didn't think of that.
     
    Raisito likes this.