How can I make my Player Game Object face it's Direction of Movement

Hi,
I know this question has been asked many times before but I cannot get it working with my 2D game. Does anybody know how I can make a certain game object such as the player face the direction it is moving. Here is my code for the movement:

    transform.Translate (Vector3.up * maxSpeed * Time.deltaTime * inputDevice.LeftStickY, Space.World);
    transform.Translate (Vector3.right * maxSpeed * Time.deltaTime * inputDevice.LeftStickX, Space.World);

As you can see I have set a certain movement speed called “maxSpeed”. Also, you can see I have inputDevice.LeftStickX and inputDevice.LeftStickY in my code. They work just like GetAxis(“Horizontal”) and GetAxis(“Vertical”) and are from a Unity Plugin called InControl. If anybody can help me with my problem it would be much appreciated.

Many Thanks,

Tommy Eaves

You could simply save the position before, and after the movement. Then, you will have the direction :

Vector3 position = transform.position ;
Vector3 direction ;
transform.Translate (Vector3.up * maxSpeed * Time.deltaTime * inputDevice.LeftStickY, Space.World);
transform.Translate (Vector3.right * maxSpeed * Time.deltaTime * inputDevice.LeftStickX, Space.World);
direction = transform.position - position ;

Then, you use the LookRotation(Vector3 forward, Vector3 upwards = Vector3.up); function to rotate your object :

transform.rotation = Quaternion.LookRotation( direction ) ;

See doc : Unity - Scripting API: Quaternion.LookRotation

For 2D you should have a sprite as your character, and the sprite is an image of your character facing one direction. So if you want your character to face a different direction you would need a sprite of your character facing the desired direction.

If the original sprite is facing right you can just rotate it 180 degrees to get it to face left.

So if the horizontal axis is positive you can just move your sprite to the right. If the axis is negative then rotate the sprite and move it to the left.

transform.Rotate (Vector3.forward * -180); // try 90 if 180 doesn't work

Same for the vertical axis, you can have a sprite for moving down the Y axis and one for moving up the Y axis.

That would work for platformer or RPG type games. If that is not how your game is setup the. Let me know.

Hey, i had the same problem and i solve it this way about the scale with a bool

isLookingRight = true;

float hor = Input.GetAxis (“Horizontal”);
rigbod2d.velocity = new Vector2 (hor * maxSpeed, rigbod2d.velocity.y);
if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight))
Flip ();

and the flip() looks this like

void Flip()
{
lookingRight = !lookingRight;
Vector3 myScale = transform.localScale;
myScale.x *= -1;
transform.localScale = myScale;

}