Question by
psykojello2 · Sep 24, 2015 at 06:39 AM ·
accelerometergyroscopetilt
Move sprite in Y based on phone tilt
I know there are many posts about this, but I haven't found a solution that works in my case. Its probably because I'm just really bad at rotations and quaternion/euler math :(
Here's what I need: I have a 2D sprite character on my screen. My phone is in landscape mode. When I tilt my phone forward and backwards, I want the character to move up and down on the Y axis.
It would be great if this could work even if the user was lying on his back or side. Also, is the code different for iOS and Android?
Here's what I have :
void Callibrate()
{
initialOrientation = Input.acceleration;
}
void Update ()
{
currentOrientation = Input.acceleration; //In my code I filter this to prevent noise.
diff = currentOrientation.y - initialOrientation.y;
}
On Android: for normal usage, diff is positive when tilted forward and negative when tilted back. When upside down (lying down), diff is negative for forward and positive for back.
Haven't tested iOS yet.
Comment