- Home /
Question by
djindepth12 · Nov 20, 2012 at 04:09 AM ·
iphoneaccelerometerunity remote
Unity Remote - iPhone Orientation
I am using this:
dir.z = Input.acceleration.y;
to translate the y angle of my iphone into z transform of a game object. At present my phone must be held in the wide screen orientation to achieve a null.
Question: How could achieve the same result from a portrait (vertical) orientation?
Here's the whole code sample below. // Move object using accelerometer var speed = 10.0; var mag:float = 2;
function Update ()
{
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
//dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.y;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > mag )
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
}
Comment
Best Answer
Answer by djindepth12 · Nov 20, 2012 at 04:03 PM
I hacked the dir with this: dir.z = -(Mathf.Abs(Input.acceleration.y - 1)); This results in all rotations on the y axis translated as positive motion. It works for now. Feel free to post other suggestions.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084138im_/https://answers.unity.com/themes/thub/images/avi.jpg)