Programming is just Google, Ctrl-c, Ctrl-v!

A lot of the time the title of this post is true (for me at least). However, as I recently encountered it doesn’t always hold true. I’m working on a little pet project (another game) and I needed to know the rotation of the mobile screen. I didn’t need the tilt, yaw, pitch, facing, or any of the other myriad of axis. Just a simple rotation. Specifically, the amount of rotation between, say, Portrait and Landscape left. A quick Google showed that a number of folks had wanted similar data during their creative endeavours so I simply went to work Ctrl-c’ing, Ctrl-v’ing my way around and checking to see if the posted solutions were what I required. Many examples were overly complex especially considering that I’d have thought this was a rather common requirement for games on a mobile platform.

After a while I used some of what I’d learned from the many complex online examples, and condensed it down into something that would work for me.

System.Math.Round(Input.acceleration.y + Input.acceleration.z, 1);

Essentially adding the z and y axis got me a simple result that showed only the rotation. Previously both axis figures changed when the phone was tilted (pitch) or rotated. I’m still not sure if this is a “feature” of my phone or it will remain the same across all devices. The one caveat that I can easily overcome is that the above code shows the same results regardless of whether the device is tilted to the left or right.

With an embarrassing amount of time spent figuring out the above I set about coding the left/right adjustment and discovered Input.gyro.gravity function… UGH!

Input.gyro.gravity.x;

Does everything I need….

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.