Code for rotating in direction of motion with the mouse not working when switched to accelerometer.

HERE is my previously answered question to achieve the rotation when the mouse is in question. I tried to convert the same code to control the object using the Accelerometer, but neither does the object move in the direction of the tilt, nor is it rotating in that direction. Here is my current code for the accelerometer.

void Update(){	
		Vector3 dir = Input.acceleration;
		if (dir.sqrMagnitude > 1)
			dir.Normalize();
		dir *= Time.deltaTime;
		transform.Translate(dir * speed);
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
		transform.rotation = Quaternion.AngleAxis(angle+90.0f, Vector3.forward);	
}

If anyone else has any suggestions for this, I request you to kindly add them. Thanks !

I would think the problem comes from the fact that the accelerometer uses a different coordinate system

If your world is flat, then the y axis of the accelerometer is the forward (z) axis of your object.

Here is how we were doing for a game using the accelerometer to move a mouse toy object:

void Update () {
	Vector3 planarVelocity;
	rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,0,Input.acceleration.y*speed);
	planarVelocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);

	if (planarVelocity.magnitude > 0.5f)
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(planarVelocity), rotSpeed*Time.deltaTime);
    }
}

There are a few variables you need to create and define but else it is quite straightforward approach.

The acceleration is used to define the velocity of the rigidbody, this, compares to Translate will creates a movement that interacts with the environment while Transalte will get you through any collider.

Then planarVelocity is just a fancy word, our programmer found to describe the direction vector…Then it is just a basic Slerp of the transform rotation.