我有一个简单的脚本(请不要说明我没有使用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;
}
}
}
答案 0 :(得分:0)
我每次在timeSet
开始时都将变量moveStuff()
声明为false。我把它作为一个私有变量移动到我宣布其他东西的区域,它完美地工作。