I hope I'm using terms that make sense. I'm a veteran 3D artist learning programming for Unity. Basically what I've got is an object that randomly selects an object from an array and instances it. That part works perfect, the next step I want is to give each object in the array a frequency value so I can make that object more or less likely to be selected.
Ideally in the inspector it would look something like this:
In the above example, gameObjectA would have a 25% chance of being selected for instancing while gameObjectC would only have a 5% chance, etc.
I'm really not sure where to start on this one. It needs to be something that I can easily expand as I'm not really sure how many elements I'll need in the end. I'm hoping what I'm looking for is just an expansion of the array function and I'm just using the wrong terms in my search. I really just need a push in the right direction.
Thanks for any help.
Add up the total frequencies once and roll that. You then convert the frequencies into a table (ex: Element1 0.25 - 0.65) or don't and slice off until you get a hit. This is C#, translated to JS where I know how, but same idea:
Converting to a table is the subtraction method, backwards. It turns your numbers into 0.25 0.65 0.7 1.0. You only have to do it once. It makes the lookups go faster (use the first item greater than your roll):
answered Nov 30 '11 at 05:01 AM