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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Critical chance system

Discussion in 'Scripting' started by Ariath, Dec 5, 2015.

  1. Ariath

    Ariath

    Joined:
    Nov 23, 2013
    Posts:
    3
    Hello, i try make some critical chance system :

    Code (CSharp):
    1.     float randValue = Random.value;
    2.     if (randValue < .90f) // 90% of the time
    3.     {
    4.         // Do Normal Attack
    5.     }
    6.    
    7.     else // 10% of the time
    8.     {
    9.         // Do Normal Attack x 2
    10.     }
    11.  
    Actually my Critical Chance are 10% , But I don't know how to increase Critical Chance.
    For example, if i do a skill system, how could I raise 5% (so 15% total) my critical chance ? I don t know how to change variable, how creta logic system...

    I guess it is simple for some of you, but i'm really noob...

    Thank's for any help ;)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. float target = 0.1f; // replace value with chance calculation
    3. float randValue = Random.value;
    4.  
    5. if (randValue < (1f-target))
    6. {...
    7.  
    8.  
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Something like this would work:

    Code (CSharp):
    1. float critChance = 0.0f;
    2.  
    3. void IncreaseCritChance(float critInc)
    4. {
    5.      critChance += critInc;
    6.  
    7.      //Never let the crit chance go out of range
    8.      if(critChance > 100.0f)
    9.      {
    10.           critChance = 100.0f;
    11.      }
    12. }
    13.  
    14. void DoAttack()
    15. {
    16.      float randValue = Random.value;
    17.      if(randValue < critChance)
    18.      {
    19.           //Do crit attack
    20.      }
    21.      else
    22.      {
    23.           //Do normal attach
    24.      }
    25. }
    This is obviously sudo code, but you can get the gist :)
     
    MikawasakiDigital likes this.
  4. Ariath

    Ariath

    Joined:
    Nov 23, 2013
    Posts:
    3
    Okay ! Many Thank's for quick answer, very helpfull ;)