使用随机性
Simple随机数
Random.valueRandom.Range圆形或球体内的随机点
Random.insideUnitCircleRandom.insideUnitSphereRandom.onUnitSphereOther类型的随机值
Random.rotationRandom.ColorHSV从 Array 中选择随机项
Random.Rangevar element = myArray[Random.Range(0, myArray.Length)];Random.RangemyArray.Length选择具有不同概率的项
- 50% 的友好问候机会
- 25% 的running几率
- 20% 的立即attack几率
- 5% 的机会提供金钱作为礼物

A strip representing a probability space from 0% to 100% is split into four segments representing 50%, 25%, 20%, and 5% of the strip's length. A dart is embedded at the 82% point along the strip, inside the 20% segment.
Random.valuefloat Choose (float[] probs) { } 浮点 总计 = 0; foreach (float elem in probs) { 总计 += elem; } float randomPoint = Random.value * total; for (int i= 0; i < probs.Length; i++) { if (randomPoint < probs[i]) { return i; } else { randomPoint -= probs[i]; } } return probs.Length - 1;}
Random.valueif (randomPoint < probs[i])Continuous随机的加权
AnimationCurve浮点 CurveWeightedRandom(AnimationCurve 曲线) { return curve.Evaluate(Random.value);}
Random.valuecurve.Evaluate()
A linear curve does not weight values at all; the horizontal coordinate is equal to the vertical coordinate for each point on the curve.

This curve is shallower at the beginning, and then steeper at the end, so it has a greater chance of low values and a reduced chance of high values. You can see that the height of the curve on the line where x=0.5 is at about 0.25, which means there's a 50% chance of getting a value between 0 and 0.25.

This curve is shallow at both the beginning and the end, making values close to the extremes more common, and steep in the middle which will make those values rare. Notice also that with this curve, the height values have been shifted up: the bottom of the curve is at 1, and the top of the curve is at 10, which means the values produced by the curve will be in the 1-10 range, instead of 0-1 like the previous curves.
AnimationCurveMathf.RoundToInt()刷新列表
void Shuffle (int[] deck) { for (int i = 0; i < deck.Length; i++) { 整型 temp = 整型 [i]; 整型 randomIndex = 范围 deck[i] = deck[randomIndex]; deck[randomIndex] = temp; }}
从一设置没有重复的项中进行选择
变换[] spawnPoints;变换[] ChooseSet(整型 numRequired) { 变换 result = 新建 变换]; 整型 numToChoose = numRequired; for (int numLeft = spawnPoints.Length; numLeft > 0; numLeft--) { float prob = (float)numToChoose/(float)numLeft; if (Random.value <= prob) { numToChoose--; result[numToChoose] = spawnPoints[numLeft - 1]; if (numToChoose == 0) { break; } } } return result;}
Space 中的随机点
Random.valuevar randVec = Vector3(Random.value, Random.value, Random.value);
Random.insideUnitSpherevar randWithinRadius = Random.insideUnitSphere * radius;
Random.insideUnitCirclevar randWithinCircle = Random.insideUnitCircle * radius;