Character faces wrong direction while moving

I had similar problems when I was programming Pong with C++ but as it is, my character move is to move in four directions. Up, down, right, and left whilst facing the direction that he’s moving in. The character faces the correct angle while moving left or right but also ends up facing left or right whilst moving up or down. The code was adapted from this video:

#pragma strict

var forward : Vector3;
var right : Vector3;
var dt : double;

var leftRotation : float;
var rightRotation : float;
var upRotation : float;
var downRotation : float;

var suspended : boolean;
var moveSpeed : double;
 
function Start ()
{ 
    
    forward = new Vector3(0, 0, 1);
    right = new Vector3(1, 0, 0);
    dt = Time.deltaTime;
    
    leftRotation = -0.7071069;
    rightRotation = 0.7071069;
    upRotation = -0.7071069;
    downRotation = 0.7071069;
    
    suspended = false;
    moveSpeed = 3.0;
}

function Update ()
{
       if (suspended)
       {
          return;
       }
       
       if (Input.GetKey(KeyCode.LeftArrow))
       {
         transform.rotation.y = leftRotation;
         transform.Translate (forward * moveSpeed * dt);
         animation.Play("run");
       }
       
       else if (Input.GetKey(KeyCode.RightArrow))
       {
         transform.rotation.y = rightRotation;
         transform.Translate (forward * moveSpeed * dt);
         animation.Play("run");
       }
       
       if (Input.GetKey(KeyCode.UpArrow))
       {
         transform.rotation.y = upRotation;
         transform.Translate (right * moveSpeed * dt);
         animation.Play("run");
       }
       
       else if (Input.GetKey(KeyCode.DownArrow))
       {
         transform.rotation.y = downRotation;
         transform.Translate (right * moveSpeed * dt);
         animation.Play("run");
       }
       
}
 
function SetSuspension (setting : boolean)
{
   suspended = true;
}

1 Answer

1

You are using the same axis to rotate up and down. You need to switch the down and up transform.ratation to x or z not y because that is the axis you are using to turn left and right.

UPDATE
Sorry I misunderstood you.

Instead of using your own decimal values and entering everything in yourseld you should just use what unity has available.[here][1]

var rotateSpeed = 10;
 function Update 
{
transform.Rotate(Vector3.right * Time.deltaTime * rotateSpeed);
}

this will rotate your character 90 degrees to the right. You can change it to lef by making it negative transform.Rotate(-Vector3.right * Time.deltaTime * rotateSpeed);

up and down is the same thing just check the link it will explain it really well.
[1]: Unity - Scripting API: Transform.Rotate

Perhaps I didn't really make clear what I'm trying to do. I'm trying to get the character on a flat plane to move forward, backward, left, or right and face the direction that he's moving. If I used the 'x' axis he'd run upward into the air and defy physics. As it is though he moves in all of the correct directions with the code I pasted but only faces the correct direction for the leftkey and rightkey movements. Ever see the way the charaters move in games like FFVIII? That's basicly what I'm trying to do.