Say I’d like to randomly set a boolean to true if an object is hit by a raycast. But I’d like to be able to adjust the chance. I was looking over Random.value. Which, returns a random number between 0.0 to 1.0. Could that represent a random value between 0% and 100% chance? How do I go about this?
var myBool = (Random.value < 0.5);
C#:
public void passFail(float fChanceOfSuccess) {
float fRand = Random.Range(0.0f,1.0f);
if (fRand <= fChanceOfSuccess)
return true;
return false;
}
my solution with int:
private void Start()
{
for (int i = 0; i < 50; i++)
{
int t = UnityEngine.Random.Range(1, 101);
print(t);
print(RandomBool(ref t));
tiles*.SetActive(RandomBool(ref t));*
}
}
//dont do that please dont
private bool RandomBool(ref int value)
{
return value % 2 == 0;
}
//you can also
private bool RandomBool()
{
int value = UnityEngine.Random.Range(1, 101);
return value % 2 == 0;
}
your choice;)