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