Move character in direction its facing

Hello. As the title says, I am aiming towards having the hero face the correct direction they are moving. So far I have this-

var speed = 3 ;
var gravity = 20;



private var moveDirection = Vector3.zero;


function Update (){

var controller : CharacterController = GetComponent(CharacterController);

var forward = transform.TransformDirection(Vector3.forward);


moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);




    if (Input.GetKey("a"))
        Move(Vector3.left);
    if (Input.GetKey("d"))
        Move(Vector3.right);
    if (Input.GetKey("w"))
        Move(Vector3.forward);
    if (Input.GetKey("s"))
        Move(Vector3.back);
		
		
		
		


transform.forward = Vector3.Normalize( Vector3(Input.GetAxis("Horizontal"), transform.localRotation , Input.GetAxis("Vertical")));


/*
 if (Input.GetKeyDown("w"))
        transform.forward = new Vector3(0, 0, 1);
    else if (Input.GetKeyDown("s"))
        transform.forward = new Vector3(0, 0, -1);
    else if (Input.GetKeyDown("a"))
        transform.forward = new Vector3(-1, 0, 0);
    else if (Input.GetKeyDown("d"))
        transform.forward = new Vector3(1, 0, 0);
*/
		
		
}




function Move(direction : Vector3){
var controller : CharacterController = GetComponent(CharacterController);
controller.SimpleMove(direction * speed );
}

This works in theory, but the player always snaps to zero rotation when no keys are pressed, and I get infinite get error messages saying ‘Look rotation viewing vector
is zero’ (me thinks it’s because it’s in the update function).

How I stop the player from snapping back to zero rotation when still, and stop the error pop ups?

One more question… So far this works fine until I rotate the camera. The controls seem to only work on the world axis, so when the cam is infront or anywhere besides directly behind the player, the controls ‘reverse’. How do I get this to work by the cameras position relative to the player?

Thanks

You should also be getting a warning, since passing a zero vector to Vector3.Normalize() will give one. That should clue the solution: do not change transform.forward if the axes are zero (or close to zero, if they’re set up as analog):

var dir = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
if (dir != Vector3.zero)
    transform.forward = Vector3.Normalize(dir);

(I’ve no idea what you were trying to do passing transform.localRotation for the Y direction)