Problem in moving between lanes.

Hello everyone, I am making a game where you have 3 lanes that you can move around , a left one, a middle one and a right one, I managed to make this code here :

var veloc : float = 5;
var porco : GameObject;
var faixa1 : GameObject;
var faixa2 : GameObject;
var faixa3 : GameObject;
var faixaAtual : int = 0;
//-1 -> Esquerda    0 -> Meio   1 -> Direita 

function Update(){   
     transform.Translate(Vector3.forward * veloc / 2);
     if(faixaAtual > -2 && faixaAtual < 2){
 
        if(faixaAtual == -1){   
           if(Input.GetKeyDown(KeyCode.RightArrow)){
              faixaAtual = 0;
              porco.transform.position = Vector3(faixa2.transform.position.x,
                     2.5, porco.transform.position.z);
            }
          }
          if(faixaAtual == 0){
             if(Input.GetKeyDown(KeyCode.LeftArrow)){
                faixaAtual = -1;
                porco.transform.position = Vector3(faixa1.transform.position.x,
                   2.5, porco.transform.position.z);
             } 
             if(Input.GetKeyDown(KeyCode.RightArrow)){
               faixaAtual = 1;
               porco.transform.position = Vector3(faixa3.transform.position.x,
                  2.5, porco.transform.position.z);
             }
         }
         if(faixaAtual == 1){
           if(Input.GetKeyDown(KeyCode.LeftArrow)){
              faixaAtual = 0;
              porco.transform.position = Vector3(faixa2.transform.position.x,
                 2.5, porco.transform.position.z);
          }
       }
     }
}

but when I am moving between the “-1” lane to the “0” lane, the character skips to the “1” lane, and I can’t figure out how to fix it! Any help would be appreciated!

observation: I’m brazillian , and my variables are in portuguese, so here is a tradution of the variables : (faixa = lane) (veloc = speed) (porco = pig) (faixaAtual = currentLane).

I don’t know if this is a paste issue or if this is how you format your code, but this style makes it very difficult to read and debug. Here is your code reformatted:

function Update(){
	transform.Translate(Vector3.forward * veloc / 2);
	if(faixaAtual > -2 && faixaAtual < 2){
		if(faixaAtual == -1){
			if(Input.GetKeyDown(KeyCode.RightArrow)){
				faixaAtual = 0;
				porco.transform.position = Vector3(faixa2.transform.position.x, 2.5, porco.transform.position.z);
			}
		} 
		
		if(faixaAtual == 0){
			if(Input.GetKeyDown(KeyCode.LeftArrow)){
				faixaAtual = -1;
				porco.transform.position = Vector3(faixa1.transform.position.x, 2.5, porco.transform.position.z); 
			}
			if(Input.GetKeyDown(KeyCode.RightArrow)) {
				faixaAtual = 1;
				porco.transform.position = Vector3(faixa3.transform.position.x, 2.5, porco.transform.position.z);
			}
		}
		
		if(faixaAtual == 1){
			if(Input.GetKeyDown(KeyCode.LeftArrow)){ 
				faixaAtual = 0;
				porco.transform.position = Vector3(faixa2.transform.position.x, 2.5, porco.transform.position.z);
			}
		}
	}
}

And now the issue becomes obvious. You have no else clauses between the three if checks of ‘faixaAtual’. So when you set ‘faixAtual = 0’ in the first if clause, it then makes the second ‘if’ clause true (which is then also executed), which by the way also make the third true, but since there is no right arrow, nothing happens. While it is not strictly correct from an indention standpoint, you can fix your code by:

function Update(){
	transform.Translate(Vector3.forward * veloc / 2);
	if(faixaAtual > -2 && faixaAtual < 2){
		if(faixaAtual == -1){
			if(Input.GetKeyDown(KeyCode.RightArrow)){
				faixaAtual = 0;
				porco.transform.position = Vector3(faixa2.transform.position.x, 2.5, porco.transform.position.z);
			}
		} 
		
		else if(faixaAtual == 0){
			if(Input.GetKeyDown(KeyCode.LeftArrow)){
				faixaAtual = -1;
				porco.transform.position = Vector3(faixa1.transform.position.x, 2.5, porco.transform.position.z); 
			}
			if(Input.GetKeyDown(KeyCode.RightArrow)) {
				faixaAtual = 1;
				porco.transform.position = Vector3(faixa3.transform.position.x, 2.5, porco.transform.position.z);
			}
		}
		
		else if(faixaAtual == 1){
			if(Input.GetKeyDown(KeyCode.LeftArrow)){ 
				faixaAtual = 0;
				porco.transform.position = Vector3(faixa2.transform.position.x, 2.5, porco.transform.position.z);
			}
		}
	}
}