Sha*_*Man 0 c# random unity-game-engine
当我的敌人死亡时,我用它来播放死亡动画:
transform.GetChild(0).GetComponent<Animator>().Play("Death_01");
Run Code Online (Sandbox Code Playgroud)
我想让代码在“Death_01”或“Death_02”之间进行选择。
做到这一点最简单的方法是什么?(数组、随机数、OR、..)
使用 UnityRandom.Range来执行此操作。
int rand = Random.Range(0, 2);
if (rand == 0)
{
transform.GetChild(0).GetComponent<Animator>().Play("Death_01");
}
if (rand == 1)
{
transform.GetChild(0).GetComponent<Animator>().Play("Death_02");
}
Run Code Online (Sandbox Code Playgroud)
编辑:
做到这一点最简单的方法是什么?(数组、随机数、OR、..)
如果您有超过 2 个动画,则可以使用 array 和 的组合Random.Range。这使得它更加健壮,而不是使用一堆if语句。
//在某处将动画数组声明为全局变量
string[] allAnimation = { "Death_01", "Death_02", "Death_03", "Death_04", "Death_05", "Death_06" };
Run Code Online (Sandbox Code Playgroud)
现在,您可以执行以下操作:
int rand = Random.Range(0, allAnimation.Length);
string animToPlay = allAnimation[rand];
transform.GetChild(0).GetComponent<Animator>().Play(animToPlay);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3013 次 |
| 最近记录: |