我如何从一组动作中随机选择
How Do I Choose Randomly From A Set Of Actions
所以我目前正在研究一个可以自由漫游并通过 itself.to 移动到随机地点的敌人,我打算让它从设置的可能方向中选择它可以像这里一样:
如何从一组可能的操作中随机选择(增加 Y、增加 X、等)
始终感谢反馈 ;)
你要做的是做一个随机数生成器,生成一个1到8的随机数,每个数代表一个方向。它是这样的:
Random rand = new Random();
int direction;
void OnUpdate() {
direction = rand.Next(1, 9);
if (direction == 1) {
moveLeft();
}
else if (direction == 2) {
moveRight();
}
...
}
如果我把一门语言和c#混淆了,我很抱歉,因为我有一段时间没有做unity了:)
一种通用的方法是存储一个函数数组,并使用一个随机值将该数组索引为 select 要执行的函数。
像这样:
Random rand = new Random();
Action[] actions = {
() => { x += 1; y += 1; },
() => { x += 1; }
//...
};
actions[rand.Next(actions.Length)]();
虽然这可能相当繁琐,但也有一些优点。例如,可能的动作集可以动态变化。
在这种情况下,如果您只关心 select 从静态集合中提取随机向量,您实际上只需要 select 随机 deltaY 和 deltaX,其中 dX 和 dY是 { -1, 0, 1 }
Random rand = new Random();
int dY = rand.Next(-1, 2);
int dX = rand.Next(-1, 2);
x += dx;
y += dy;
我愿意:
int actions = Random.Range(0,2);
(最后一个数字没有出现,注意这里的可能性是给出两个可能的随机结果:“0 或 1”。-如果你想要“0、1 或 2”,请输入 Random.Range(0,3);
和等等)
更多关于Random Range
并使用 switch statement 检查它 - 它将触发在 int actions
中排序的确切操作 :)
switch(actions){
case 0:
//do thing1
break;
case 1:
//do thing 2
break;
default:
//do nothing
break;
}
Random rand = new Random();
int direction;
Vector3[] Directions = { // front, back, left, right and diagonal positions
new Vector3( 1 , 0 , 0 ),
new Vector3( -1 , 0 , 0 ),
new Vector3( 0 , 1 , 0 ),
new Vector3( 0 , -1 , 0 ),
new Vector3( 1 , 1 , 0 ),
new Vector3( -1 , -1 , 0 ),
new Vector3( 1 , -1 , 0 ),
new Vector3( -1 , 1 , 0 )
}
public Vector3 ChooseRandome(){
direction = rand.Next( Directions.length ); // Random number with directions.length as max possible number
return Directions[randomIndex]; // plug random number in and return index.
}
所以我目前正在研究一个可以自由漫游并通过 itself.to 移动到随机地点的敌人,我打算让它从设置的可能方向中选择它可以像这里一样:
如何从一组可能的操作中随机选择(增加 Y、增加 X、等)
始终感谢反馈 ;)
你要做的是做一个随机数生成器,生成一个1到8的随机数,每个数代表一个方向。它是这样的:
Random rand = new Random();
int direction;
void OnUpdate() {
direction = rand.Next(1, 9);
if (direction == 1) {
moveLeft();
}
else if (direction == 2) {
moveRight();
}
...
}
如果我把一门语言和c#混淆了,我很抱歉,因为我有一段时间没有做unity了:)
一种通用的方法是存储一个函数数组,并使用一个随机值将该数组索引为 select 要执行的函数。
像这样:
Random rand = new Random();
Action[] actions = {
() => { x += 1; y += 1; },
() => { x += 1; }
//...
};
actions[rand.Next(actions.Length)]();
虽然这可能相当繁琐,但也有一些优点。例如,可能的动作集可以动态变化。
在这种情况下,如果您只关心 select 从静态集合中提取随机向量,您实际上只需要 select 随机 deltaY 和 deltaX,其中 dX 和 dY是 { -1, 0, 1 }
Random rand = new Random();
int dY = rand.Next(-1, 2);
int dX = rand.Next(-1, 2);
x += dx;
y += dy;
我愿意:
int actions = Random.Range(0,2);
(最后一个数字没有出现,注意这里的可能性是给出两个可能的随机结果:“0 或 1”。-如果你想要“0、1 或 2”,请输入 Random.Range(0,3);
和等等)
更多关于Random Range
并使用 switch statement 检查它 - 它将触发在 int actions
中排序的确切操作 :)
switch(actions){
case 0:
//do thing1
break;
case 1:
//do thing 2
break;
default:
//do nothing
break;
}
Random rand = new Random();
int direction;
Vector3[] Directions = { // front, back, left, right and diagonal positions
new Vector3( 1 , 0 , 0 ),
new Vector3( -1 , 0 , 0 ),
new Vector3( 0 , 1 , 0 ),
new Vector3( 0 , -1 , 0 ),
new Vector3( 1 , 1 , 0 ),
new Vector3( -1 , -1 , 0 ),
new Vector3( 1 , -1 , 0 ),
new Vector3( -1 , 1 , 0 )
}
public Vector3 ChooseRandome(){
direction = rand.Next( Directions.length ); // Random number with directions.length as max possible number
return Directions[randomIndex]; // plug random number in and return index.
}