When adding code to make object face the direction it is moving, the accelerometer goes all wonky.

I want my object to face towards the direction it is moving, based on the accelerometer. The movement is working alright, but when I add the code to make it face the direction it is travelling towards, the accelerometer goes all wonky and the object doesn’t go where it is supposed to, based on the tilt. For example, tilting the device towards me makes it go up instead of down and tilting away from me doesn’t move the player much from where it is. What is happening and why ? Here is my code :

Vector3 dir = getAccelerometer(Input.acceleration);
		if (dir.sqrMagnitude > 1)
		dir *= Time.deltaTime;
		transform.Translate(dir * speed);

		float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
		transform.eulerAngles = new Vector3(0,0, angle);

Got it to work finally.

  Vector3 planarVelocity;
       rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,Input.acceleration.y*speed,0);
       planarVelocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
       float angle = Mathf.Atan2(rigidbody.velocity.y, rigidbody.velocity.x) * Mathf.Rad2Deg;
       if (planarVelocity.magnitude > 0.5f)
         transform.rotation = Quaternion.AngleAxis(angle(add offset if needed here), Vector3.forward);