# How to rotate character to movement direction

I am making a top down movement script and I need my character to rotate in the direction I am running. I can’t use transform.rotate because I need the character to always rotate to the left when i press “A” and so on. Here is my code so far:

``````#pragma strict

var movementSpeed = 0.5;

function Start () {

}

function Update () {

//WASD MOVEMENT
if(Input.GetKey(KeyCode.A)) {
transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.D)) {
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.S)) {
transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}

//ARROW KEY TURNING
if(Input.GetKeyDown(KeyCode.A)) {
//transform.Rotate(Vector3.back, Space.World);
transform.rotation.x += 1;
}

if(Input.GetKeyDown(KeyCode.D)) {
//transform.Rotate(Vector3.back, Space.World);
}

if(Input.GetKeyDown(KeyCode.W)) {
//transform.Rotate(Vector3.back, Space.World);
}

if(Input.GetKeyDown(KeyCode.S)) {
//transform.Rotate(Vector3.back, Space.World);
}
}
``````

Hi there, First of all, you should use EulerAngles to rotate your char NESW north east south west, or never eat shredded wheat whichever you prefer…

``````A = transorm.eulerAngles = (0,0,0)
or player.transform.rotation = quaternion.eulerAngles (0,90,0)
A = transorm.eulerAngles = (0,90,0)
A = transorm.eulerAngles = (0,180,0)
A = transorm.eulerAngles = (0,360,0)
``````

so you have the quaternion rotations that you want in easy format. then you use Slerp to rotate between current angle and new one, and try different lerp speeds to find the right one.

you can also use transform.lookAt (0,99999,0) if you want, to point your player to different angles on screen.

One solution is to save the position of the previous frame and use the change between the frames as a direction. Below is an untested rewrite to your rotation code. It assumes 1) 2D (since you are moving on the XY plane, and 2) that your sprite or quad has the front side on the right when the rotation is (0,0,0).

``````#pragma strict

var movementSpeed = 0.5;

private var prevPos : Vector3;

function Start () {
prevPos = transform.position;
}

function Update () {

//WASD MOVEMENT
if(Input.GetKey(KeyCode.A)) {
transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.D)) {
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}

if(Input.GetKey(KeyCode.S)) {
transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}

if (prevPos != transform.position) {
dir = transform.position - prevPos;
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
prevPos = transform.position;
}
}
``````

Note that this code causes an immediate rotation change. You could add Slerp()ing code, but then the object would be out of alignment for some period of the move in a new direction.

Unknown identifier “dir”