是一个unity新手,最近摸索unity想出了一个新的方法
unity是在每帧执行update,但是假如我要让角色重复执行10次前进然后攻击然后继续前进10次然后再攻击形成循环就不能直接用for10次然后攻击然后外面套个while
于是我定了一个int变量 state,如果state==0且前进次数少于10则前进然后增加前进次数,否则如果前进次数大于等于10则将攻击次数归0然后将state设为1,否则如果state==1则攻击然后将state设为0
大概就是这样:
private int state=0;
private int moveTimes=0;
private void Update()
{
if(state==0&&moveTimes<10) {
MoveUp();
}
if(state==0&&moveTimes>=10) {
state=1;
attackTimes=0;
}
if(state==1){
Attack();
state=0;
}
}