Rotate character in the direction it is walking


I’m making a top down mobile game and was wondering if is was possible to let the character face the direction it is walking in. The camera will remain at the same position, it is a child object of the character so it will follow him along. I have animation attached.

I’m using virtual controls suite as the joystick and then transform the character to walk in the direction the joystick is pointing.

Here is my code so far (not that big :D)

	public GameObject player;
	public float moveSpeed = 5.0f;

	void Update () {

		VCAnalogJoystickBase joy = VCAnalogJoystickBase.GetInstance("stick");

		//move the player character according to the joystick, arguments of translate are x,y,z
		if (joy != null){
			player.transform.Translate(moveSpeed * Time.deltaTime * joy.AxisX, 0.0f, moveSpeed * Time.deltaTime * joy.AxisY);

		//play the animations

		if (joy.AxisX == 0.0f && joy.AxisY == 0.0f){

			animation["Run"].speed = 15.0f;

The parameters of Translate form a Vector: Vector2(moveSpeed * Time.deltaTime * joy.AxisX, moveSpeed * Time.deltaTime * joy.AxisY) (Vector2 as one component is always 0).

You can use this vector to calculate the angle:

Vector2.Angle(a, b);

Where one of the vectos is a base vector (Vector2.right etc.)

You can set the angle using

transform.eulerAngles = new Vector3(angle, 0f, 0f);

Things you need to figure out for yourself:

  • Which base vector to use (might be left, up oder -up [there is no down!?])
  • which of the eulerAngles you want to set