How does Random.value work?

I’m trying to script it to where there is a chance that when a character’s chest collider (for example) is hit by a raycast, a specific animation might play. I think I’m doing it wrong, though. Using this as a test:

 if(hit.collider.CompareTag("EnemyChest")){
    
    				if(Random.value < 0.75f){
    					Debug.Log("Chest animation!");
      }
}

I figured that line of script would say “if the random.value is greater than 75%, play animation” but it returns positive after every hit. Even if I lower the value down to 0.1. What am I misunderstanding here?

1 Answer

1

Random.value returns a new random number between 0.0 and 1.0 inclusive each time it is assigned. So your code will print Debug.Log() 75% of the time. Note if you are calling it over multiple frames, you would get a 75% chance per call. What puzzles me is this statement, “Even if I lower the value down to 0.1,” because that should give you a 10% chance each time this code is executed. You might want to put an else in so that you get output when it fails as well.

Turns out this is actually my mistake. I apparently forgot the correct symbols for greater than and less than...thanks for your input though!