- Home /
Question by
TakMarche · Jul 14, 2013 at 03:48 AM ·
androidaccelerometertilt
Face Direction of Tilting
I'm currently working on a 2D game where the player can move along the x and y axis based on the accelerometer. I'm currently using this to get that done:
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
if (dir.sqrMagnitude > 1)
{
dir.Normalize();
}
dir *= Time.deltaTime;
transform.Translate(dir * speed);
What I am trying to figure out next is how to get the player to face the direction it's tilting towards (z axis for rotation). Googled up many result but I can't seem to find a solution.
Comment
Best Answer
Answer by robertbu · Jul 14, 2013 at 05:28 AM
I think this will do what you want:
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0,0,angle);
This will face the right side towards 'dir'. If you need another side, add or subtract 90x to the angle before creating the vector.