- Home /
Unity random card select system with persentage
Hello, im trying to make simple card game for learn myself. and i have question on random persentage system.
I have 100 prefab of normal card.
I have 1 prefab of special card.
every signed time get whatever card to put on my cardSet list.
When the player make mouse button click the card appear in windows
I want to in special variables(like when my happyness are less than 20) the Special card sould be appear with Weighted average in my cardSet list
how i make that?
here are my scripts.
public List<GameObject> cardSet = new List<GameObject>();
void Update()
{
timeCount += Time.deltaTime;
if(timeCount >= cardTime && cardSet.Count <= maxCardSave)
{
SpawnCard();
timeCount = 0;
}
if (Input.GetMouseButtonUp(0))
{
cardSet[0].transform.SetParent(canvas2.transform,false);
cardSet[0].transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.InBounce);
}
}
void SpawnCard()
{
int randomCard = Random.Range(0, 3);
cardSet.Add(Instantiate(totalCard[randomCard])as GameObject);
}
Comment
Answer by JPhilipp · Feb 21, 2020 at 09:15 AM
You can do
void SpawnCard()
{
float chanceForSpecialCard = happiness < 20f ?
70% : 100f / totalCard.Count;
const int specialCardIndex = 2;
int randomCard = Chance(chanceForSpecialCard) ?
specialCardIndex : Random.Range(0, totalCard.Count);
cardSet.Add(Instantiate(totalCard[randomCard]) as GameObject);
}
bool Chance(float percent = 50f)
{
return UnityEngine.Random.Range(0, 100) <= percent;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612223722im_/https://answers.unity.com/themes/thub/images/avi.jpg)