Unity JS - simple if statements not behaving as expected?

Posted by IHazABone on Stack Overflow See other posts from Stack Overflow or by IHazABone
Published on 2013-10-17T04:25:33Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 168

Filed under:
|
|

I have a simple script (please no remarks on the fact that I'm not using a switch statement or better code, this is the earliest version and written this way by a peer, I am improving it) that takes an object and moves it back and forth. For some reason, the variable time gets stuck at 249. It is probably an obvious bug with this inefficient logic, but I cannot seem to find it.

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;
                }
            }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about if-statement