Rotate character to the moving direction problems?

Hi, I made a simple character controller script here :

var speed : float = 6.0;

var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var target : Transform;
private var moveDirection : Vector3 = Vector3.right;

function Update() {

var controller : CharacterController = GetComponent(CharacterController);
// I use "target" as a world space reference, will that be a problem?
moveDirection = Vector3.Normalize(new  Vector3(Input.GetAxis("Horizontal"), 0f, 
moveDirection *= speed;

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);

transform.LookAt(target.position + moveDirection);


It uses the horizontal/Vertical inputs as movement, and rotates to the moving direction. It works well when you use the WASD keys to move around ( the object rotates to the moving direction ), but when you release the directional keys, the object immediately bounces back to (0,0,0) rotation… how do I make the object remain facing that moving direction even when you stopped pressing the buttons? Thanks!

Im stuck here and I really need some help guys...

The problem is that when you release the WASD keys, your “transform.LookAt(target.position + moveDirection);” code line will read the (0,0,0), because Input.GetAxis´s calls either vertical or horizontal will always be 0 when no keys are pressed.

My quick and dirty suggestion is that you have a second variable. Always copy the moveDirection to it right after you calculate the moveDirection. Then, you apply LookAt function with moveDirection only when there are WASD keys pressed. Other than that, you apply lookAt with that secondary variable which will happen to have the last read Input.GetAxis´s values before the keys were released.