0

我有一个简单的脚本(请不要评论我没有使用 switch 语句或更好的代码,这是最早的版本,由同行以这种方式编写,我正在改进它)它接受一个对象并移动它来回。由于某种原因,该变量time卡在 249。这可能是这种低效逻辑的一个明显错误,但我似乎找不到它。

var speed = 1;
private var time = 0;

function Start() {
}

function Update() {
    if(condition == true)moveStuff();
}

function moveStuff() {
    var timeSwitch = false;
    if(time == 0)timeSwitch = false;
    if(time == timeSet)timeSwitch = true;

        if(direction == 1) {
            if(timeSwitch == false) {
                    transform.Translate(Vector3.up * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log(time);
                }else if(timeSwitch == true) {
                    transform.Translate(Vector3.up * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                    Debug.Log(time);
                }   
            } else if(direction == 2) {
            if(timeSwitch == false) {
                    transform.Translate(Vector3.down * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log("Moved down. ");
                }else if(timeSwitch == true){
                    transform.Translate(Vector3.down * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                }
            } else if(direction == 3) {
                if(timeSwitch == false) {
                    transform.Translate(Vector3.forward * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log("Moved forward. ");
                }else if(timeSwitch == true){
                    transform.Translate(Vector3.forward * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                }
            } else if(direction == 4) {
                if(timeSwitch == false) {
                    transform.Translate(Vector3.back * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log("Moved back. ");
                }else if(timeSwitch == true){
                    transform.Translate(Vector3.back * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                }
            } else if(direction == 5) {
                if(timeSwitch == false) {
                    transform.Translate(Vector3.right * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log("Moved right. ");
                }else if(timeSwitch == true){
                    transform.Translate(Vector3.right * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                }
            } else if(direction == 6) {
                if(timeSwitch == false) {
                    transform.Translate(Vector3.left * (Time.deltaTime * speed));
                    time += 1;
                    Debug.Log("Moved left. ");
                }else if(timeSwitch == true){
                    transform.Translate(Vector3.left * ((Time.deltaTime * speed) * -1));
                    time -= 1;
                }
            }
}
4

1 回答 1

0

timeSet每次开始时,我都将变量声明为 false moveStuff()。我把它作为一个私有变量移到了我声明其他东西的区域,它运行良好。

于 2013-10-17T21:51:33.480 回答