Help with third person character controller rotation

Hi, I have a third person character controller that works very much like Lerpz, the problem is that when I stop giving input the rotation gos back to zero.

Here’s the script:

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

var speed = 6.0;
var gravity = 20.0;
var doesRotate = false;

function Update () {
// move x or z relative to the camera
if (grounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = Camera.main.transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (doesRotate) {
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
		
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

Does anyone know what the problem is?

“var doesRotate = false;”

doesRotate never gets set to true…so,

“if (doesRotate) {
transform.rotation = Quaternion.LookRotation(moveDirection);
}”

never executes…

You need to set does rotate to true somewhere!

if i understood correctly, i’d bet you just need to cache the last rotation.

private var lastRot : Quaternion;

function Start(){
lastRot = transform.rotation; // init it in start so it's not null
}

function Update () {
transform.rotation = lastRot;// first line in update so it's the last known rotation

...

if (doesRotate) {
transform.rotation = Quaternion.LookRotation(moveDirection);
lastRot = transform.rotation;
}