walk rotation delay

Hi,

We have a problem with our script for our main character. We’ve justed a standard script for the controller.
the game is 3d platform but the camera is 2d.

but when you want to walk the opposite direction there is a delay of 1 or 2 seconds and than it makes a big turn to go the opposite direction.

if you use the up and down buttons also it does makes the turn immediately.

btw script noob ^^’

var speed = 3.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

var smoothSpeed = 10.0;
var smoothDirection = 10.0;

var canJump = true;

private var moveDirection = Vector3.zero;
private var verticalSpeed = 0.0;
private var moveSpeed = 0.0;

private var grounded : boolean = false;
private var jumping : boolean = false;

private var targetAngle = 0.0;

// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)

function Awake ()
{
	moveDirection = transform.TransformDirection(Vector3.forward);
}

function UpdateSmoothedMovementDirection ()
{

	var cameraTransform = Camera.main.transform;
	
	// Forward vector relative to the camera along the x-z plane	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;
	// Right vector relative to the camera
	// Always orthogonal to the forward vector
	var right = Vector3(forward.z, 0, -forward.x);

	// Target direction relative to the camera
	var targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;
	
	// We store speed and direction seperately,
	// so that when the character stands still we still have a valid forward direction
	// moveDirection is always normalized, and we only update it if there is user input.
	if (targetDirection != Vector3.zero)
	{
		moveDirection = Vector3.Lerp(moveDirection, targetDirection, smoothDirection * Time.deltaTime);
		moveDirection = moveDirection.normalized;
	}

	// Smooth the speed based on the current target direction
	var curSmooth = smoothSpeed * Time.deltaTime;
	// When in air we accelerate slower
	if (!grounded)
	{
		curSmooth *= 0.5;
	}

	moveSpeed = Mathf.Lerp(moveSpeed, targetDirection.magnitude * speed, curSmooth);
}

function Update() {

	UpdateSmoothedMovementDirection();

	if (grounded) {
		verticalSpeed = 0.0;
			
		
		// Jump		
		if (canJump  Input.GetButton ("Jump")) {
			verticalSpeed = jumpSpeed;
			jumping = true;
			SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
		}
	}
	// Apply gravity
	verticalSpeed -= gravity * Time.deltaTime;
	
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0);
	movement *= Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(movement);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;

	// Set rotation to the move direction	
	transform.rotation = Quaternion.LookRotation(moveDirection);
	
	// We are in jump mode but just became grounded
	if (grounded  jumping)
	{
		jumping = false;
		SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
	}
}

function GetSpeed () {
	return moveSpeed;
}

function IsJumping () {
	return jumping;
}

function GetDirection () {
	return moveDirection;
}

Firstly, you should use Vector3.Slerp to interpolate rotations - Vector3.Lerp is used to move in a straight line between positions.

Also, the last parameter to the lerp/slerp functions is a value between 0 and 1 that specifies how far through the interpolation you are (0 is the start position, 1 is the end). You seem to be using Time.deltaTime, which is the time since the last frame update, and will not work as you expect. You can use something like:-

(Time.time - startTime) / totalAnimTime

…to get a value between zero and one suitable for use with the lerp/slerp functions.