Drop Rate system

hello guys,
i have been thinking of making a drop rate system ,
i came up with this

public class MeteorSummon : MonoBehaviour
{
public GameObject[] meteor;
public float fallSpeed;
public float timeToStart;
public float throwSpeed;
public int[] chances;

private int newNumber;
private System.Random r = new System.Random();
private int currentChance;

void Start()
{
    InvokeRepeating("summon", timeToStart, throwSpeed);
}
void Update()
{
    currentChance = r.Next(100);
}
void summon()
{
    bool dropped = false;
    for (int i = 0; i < chances.Length; i++)
    {
        if (chances[i] == currentChance)
        {
            if (chances[i] == 90)
            {
                newNumber = 3;
            }
            if (chances[i] == 80)
            {
                newNumber = 4;
            }
            if (chances[i] == 95)
            {
                newNumber = 5;
            }
            if (chances[i] == 50)
            {
                newNumber = 0;
            }
            if (chances[i] == 40)
            {
                newNumber = 1;
            }
            if (chances[i] == 30)
            {
                newNumber = 2;
            }
            throwItem(newNumber);
            print("Item with " + chances[i] + " chance is dropped");
            dropped = true;
            break;
        }
    }
    if (dropped == false)
    {
        throwItem(0);
        print("Dropping default item");
    }
}
void throwItem(int num)
{
    GameObject newMeteor = Instantiate(meteor[num], new Vector3(Random.Range(edges.leftEdge.x, edges.rightEdge.x), this.transform.position.y), Quaternion.identity) as GameObject;
    newMeteor.GetComponent<Rigidbody2D>().velocity = new Vector2(0f, fallSpeed);
    newMeteor.transform.SetParent(transform);
}

}

this works , however the drop chances are not real … 30% occurs more often than a 90%

any suggestion to improve this i’d be grateful :slight_smile:

1 Like

Use the random number generation scheme.

int v=UnityEngine.Random.Range(0,100);
if (v>90) //Do stuff
else if (v>70) //Do other stuff

Something like that. Your code is probably broken because it relies on hitting an exact number, which usually isn’t the best thing. Try for a range, such as I showed. Look at the concept of a Cumulative Distribution Function, they are quite helpful in piecing together how to make this work. Good luck!

4 Likes

thanks for the tips , i found this interesting article on unity docs,
http://docs.unity3d.com/Manual/RandomNumbers.html

it did help :wink:

1 Like

Privacy & Terms