# Rotate 2d object to facing direction

Hello Unity Community, I have some problems.

Im using accelerometer for moving my 2d sprite and the problem comes when i want to rotate my 2d sprite according to the device’s accelerometer direction.

Now my code is rotating my character but not on the z axes, only on y and x

Do you know how can i do it to rotate only on z axes and to be facing to the direction

This is my code:

``````void Update ()
{

Vector3 move = Vector3.zero ;
move.x= Input.acceleration.x;
move.z = Input.acceleration.y;

transform.Translate(Vector3.right * (move.x * maxSpeed) * Time.deltaTime,Space.World);
transform.Translate(Vector3.up * (move.z * maxSpeed) * Time.deltaTime);

if (move != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(-move);
}
}
``````

Any help will be appreciated !

I cannot verify your accelerometer calculations. If the vector is right, you can do this:

``````Vector3 dir = -move;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
``````

This code assumes the front of the 2D object is on the right. If the front is up, then you need to subtract 90 from the angle before passing it to AngleAxis().