Look towards Running Direction

so far i have this, but i’m really messed up with making my character face at the direction he’s moving

var speed: float=6.0;

var jumpSpeed: float=8.0;
var gravity : float=20.0;
private var tr : int =90;
private var moveDirection : Vector3 = Vector3.zero;

function Update ()
{ var controller : CharacterController = GetComponent(CharacterController);
if(controller.isGrounded)
{
moveDirection = new Vector3(0,0,Input.GetAxis(“Horizontal”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

	if(Input.GetButton("Jump"))
	{
	moveDirection.y = jumpSpeed;
	}
}

moveDirection.y -= gravity * Time.deltaTime;

controller.Move(moveDirection * Time.deltaTime);

}

here you have it: Transform.LookAt

Your code could look like this:

gameObject.transform.LookAt(gameObject.transform.position + moveDirection);

That is the easiest way I found to achieve what you want.
Just be carefull that the rotation can be ‘jumpy’ at times (eg when you stop).

For 2D, you’re always looking left or right? In that case:

if(MoveDirection.x<0)
  transform.rotation = Quaternion.Euler(0,-90,0);
else
  transform.rotation = Quaternion.Euler(0,90,0);

There’s no way to say “facing right,” since it depends on the exact model. This assumes the model is facing “forwards” – the most common way – which would have her back to you. (0,90,0) is spun 90 degrees right. You may have to play with the numbers.

thanks guys so far ive come up with both your ideas and made this, but i’m having trouble animating it.

function FixedUpdate () 

{
if(grounded){
moveDirection = new Vector3(Input.GetAxis(“Horizontal”),0,0);
moveDirection*=speed;

		if(Input.GetButton("Jump"))
		{
		moveDirection.y=jumpSpeed;
	
		}
	}

else
{	
	moveDirection = new Vector3(Input.GetAxis("Horizontal"),moveDirection.y/speed,0);
	moveDirection*=speed;
}


moveDirection.y-=gravity*Time.deltaTime;

controller = GetComponent(CharacterController);
flags = controller.Move(moveDirection*Time.deltaTime*speed);

grounded = (flags & CollisionFlags.CollidedBelow) !=0;



if(moveDirection.x>0)
{
tr = 90;
}else
if(moveDirection.x<0){
tr=270;
}
transform.eulerAngles.y-=(transform.eulerAngles.y-tr)/5;

if(Input.GetAxis("Horizontal"))
{
animation.CrossFade("MRUN",0.3);
}
else
{
animation.CrossFade("MIDLE",0.3);
}

}

“MWALK” layer is set to -1;
“MIDLE” also

“MJUMP” layer is set to 10 which im having trouble animating him to jump;