Help is needed. I want to make a system of random loot from the chest for the game, but it does not work. For example, there are 5 categories: Ordinary, Dwarf, Mystical, Epic, Legendary. The chance that a Normal 65% can fall out is Parana 45%, Mystical 25%, Epic 15%, and Legendary 5%. For example, in the future, categories of items may increase and the chances of changing. I had an idea, to attach my number to each item, and to make an array, well, I ran into a problem here, I am new to Unity3D, and the knowledge of C # is not much forgotten. There will be several chests with loot cells. For example, a regular chest has 10 cells, and in this chest 10 random items can fall out. Help please, how can such a thing be arranged in the form of a code. Thank you in advance.

1 answer 1

In my opinion the simplest, maybe not the most correct, solution is to take and generate numbers from 1 to 155, and then look if the number is in between:

  • from 1 to 65 - Обычная
  • from 66 to 110 - Рарная
  • from 111 to 135 - Мистическая
  • from 136 to 150 - Эпическая
  • from 151 to 155 - Легендарная

Naturally, the sequence of numbers can be changed and, for example, for Легендарной compared with numbers like 33, 111, 145, 1, 7 . For the rest ask your numbers

What to say about:

For example, in the future, categories of items may increase and the chances of changing.

Create a Category class in it should be the property Chance (Chance), then we take all the categories and sum up the "chances", we get the upper threshold for generating numbers

For

For example, a regular chest has 10 cells, and in this chest 10 random items can fall out.

it is possible to call for each cell in the loop the code that will return the item

UPD

 class Category { private int[] chanceValues; public Category(string name, int chance){ Name = name; Chance = chance; chanceValues = generateChanceValues(chance); } public string Name { get; private set; } public int Chance { get; private set; } private int[] generateChanceValues(int count){ int[] res = new int[count]; Random random = new Random(); for (int i = 0; i < count; count++){ res[i] = random.Next(1, 101); } return res; } public bool IsGenerateLoot(int number){ if (chanceValues == null) return false; return chanceValues.Contains(number); } } 
  • Hmm, the idea is not bad, only for me now it remains a problem to equate an object to a number. And then I somehow try to make an array so that from 1 to 155 Generates a random number. - Dipper
  • @Dipper I do not understand you. What does it mean to equate an object to a figure? - Vadim Prokopchuk
  • I'm confused. The idea was that it was a random, and gives out any 10 digits. And then there is a check if the number 59 = Key then this item spawns. - Dipper
  • The fact that the “chances” of the vehicle do not add up to 100% obviously indicates that things of different categories fall out independently, i.e. several things may fall out at the same time. And the method proposed by you implies a mutually exclusive loss of things. - AnT
  • one
    @Vadim Prokopchuk: This is already a question to ask the vehicle. But the fact that the chances are not summed up in 100% already leads to questions. What is meant? - AnT