I know there’s many thread about this, but I don’t understand yet… I have an sprite on a 2D game, were the back view (viewport) it’s the view from above:
The working code is:
//Rotaciones fijas de 90º, -90º y 180º
// 1
// 3 2
// 4
if(Input.GetKeyDown(KeyCode.LeftArrow)){
//Debug.Log ("Origen: " + direccion);
switch (direccion){
case 1:
character2.transform.Rotate(0,0,90);
//Debug.Log ("Rota 90");
break;
case 2:
character2.transform.Rotate(0,0,180);
//Debug.Log ("Rota 180");
break;
case 4:
character2.transform.Rotate(0,0,-90);
//Debug.Log ("Rota -90");
break;
}
direccion = 3;
//Debug.Log ("Destino: 3");
}
if(Input.GetKeyDown(KeyCode.RightArrow)){
//Debug.Log ("Origen: " + direccion);
switch (direccion){
case 1:
character2.transform.Rotate(0,0,-90);
//Debug.Log ("Rota -90");
break;
case 3:
character2.transform.Rotate(0,0,180);
//Debug.Log ("Rota 180");
break;
case 4:
character2.transform.Rotate(0,0,90);
//Debug.Log ("Rota 90");
break;
}
direccion = 2;
// Debug.Log ("Destino: 2");
}
if(Input.GetKeyDown(KeyCode.UpArrow)){
//Debug.Log ("Origen: " + direccion);
switch (direccion){
case 2:
character2.transform.Rotate(0,0,90);
//Debug.Log ("Rota 90");
break;
case 3:
character2.transform.Rotate(0,0,-90);
//Debug.Log ("Rota -90");
break;
case 4:
character2.transform.Rotate(0,0,180);
//Debug.Log ("Rota 180");
break;
}
direccion = 1;
//Debug.Log ("Destino: 1");
}
if(Input.GetKeyDown(KeyCode.DownArrow)){
//Debug.Log ("Origen: " + direccion);
switch (direccion){
case 1:
character2.transform.Rotate(0,0,180);
//Debug.Log ("Rota 180");
break;
case 2:
character2.transform.Rotate(0,0,-90);
//Debug.Log ("Rota 90");
break;
case 3:
character2.transform.Rotate(0,0,90);
//Debug.Log ("Rota -90");
break;
}
direccion = 4;
//Debug.Log ("Destino: 4");
}
Basically: When you press an arrow, depending on the previous facing direction, the games rotate the sprite… I think its a very complicated for what it does, I know theres a “LookAt” or “.foward” function, but I can’t get it working… how should I implement that??
Thanks!