Rotate Game when iPhone change it's Orientation

Hello,
I#m new here so a big HELLO to all…

i found most of my questions answer by other posts.
so here is the only one that’s open… i hope it’s not tooo easy to solve because than shame on me

Ok so how do i rotate my Game if the User rotate the iPhone. I want that the Top is alway the Top if you rotate the iPhone. :?

so if i use iPhoneInput.acceleration.y than it rotates continue… any Idea???

many thxxx a big kiss
kerstin

I’m still learning, but I think that maybe the solution to this is to look at the orientation of the device and then rotate the camera if the user is looking at the game upside down.

Heya - here is my code that does this… What it does is, if the user turns the iphone from horizontal (with the home button on the right) to vertical or from vertical to horizontal, it will flip the orientation of the display as the camera axis you are interested in approaches alignment with gravity. This is done by testing the dot product of the axis you are interested in against gravity. I do it as a flip instead of a continuous movement though…

inside Update or fixedUpdate:

``````if(Vector3.Dot(iPhoneInput.acceleration.normalized,Vector3(0,-1,0)) > 0.8)
{
iPhoneSettings.verticalOrientation = true;
}
else if(Vector3.Dot(iPhoneInput.acceleration.normalized,Vector3(-1,0,0)) > 0.8)
{
iPhoneSettings.verticalOrientation = false;
}
``````

if you want a continuous movement, you might try instead (in Update or FixedUpdate)

``````Camera.main.LookAt(Camera.main.transform.position + iPhoneInput.acceleration.normalized);
``````

…or something like that, haven’t tried that yet, but that should make your camera continuously look at a position that is 1 gravity vector away from it’s current position…

-jdm

Wonderfull !!!
The first one works fine like you describe…
It’ would work for me least but i like also the continuos rotation but with that i get a error: LookAt is not a member of UnityEngine.Camera

``````Camera.main.LookAt(Camera.main.transform.position + iPhoneInput.acceleration.normalized);
``````

many thx
kerstin

My bad, I think LookAt is a member of transform, so you’ll want to call it on the camera’s transform… Camera.main.transform.LookAt

FYI for unity 3.0, the calls have been changed. The previous code have been deprecated. Use something like this instead.

``````if(Vector3.Dot(Input.acceleration.normalized,new Vector3(1,0,0)) > 0.8) {
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeRight;
}
else if(Vector3.Dot(Input.acceleration.normalized,new Vector3(-1,0,0)) > 0.8) {
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
}
``````

I check Input.deviceOrientation on the iPad, but I assume it works for the iPhone, also.