About Character Control :D

Hello there~! I am wondering if anyone can tell me why my character always will face forward after finishing of control. Following are my scripts and Thanks in advance!

var moveSpeed = 5.0;

var rotateSpeed = 80.0;

private var controller :CharacterController; 

controller = gameObject.GetComponent(CharacterController);  

private var moveDirection = Vector3.zero; 

private var forward = Vector3.forward; 

private var right = Vector3.right;

function Update() { 

    var x = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed;

    var z = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;

    var targetDirection = x * right + z * forward;

    transform.Translate(x,0,z,Space.World);

    var angle = Mathf.Atan2(x,z)* Mathf.Rad2Deg ;

    transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);

}

If you have a better way of controlling it like mine and would like to share it with me, then I will very appreciate it~! :)

Don't understand your question completely but you DON'T want the character face the direction you're moving? Then remove the `transform.rotation` line at the end.

If you WANT to have it look in the right direction, you could use `transform.LookAt(transform.position + targetDirection, Vector3.up)`, although you should check if `targetDirection.sqrMagnitude > 0`. So that would give:

var moveSpeed = 5.0;
var rotateSpeed = 80.0;
private var controller :CharacterController; 
controller = gameObject.GetComponent(CharacterController);  
private var moveDirection = Vector3.zero; 
private var forward = Vector3.forward; 
private var right = Vector3.right;

function Update() { 
    var x = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed;
    var z = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;

    var targetDirection = x * right + z * forward;

    transform.Translate(x,0,z,Space.World);

    if (targetDirection.sqrMagnitude > 0) {
        transform.LookAt(transform.position + targetDirection, Vector3.up);
    }
}