CPMStar Ad

Naughty Who @ indiedb

Wednesday, February 12, 2014

[unity] Accelerometer #gamedev

More and more progress with our new #mobile #game on #Unity and we are close to one #gamedev related article per week :) Today let's discover accelerometer.

I (Artur) never had a chance to try programming logic with an accelerometer before. In general, it's not that scary and even simple: the data is stored in the Input.acceleration.  It's an example of the Vector 3 class and the gravitation data is contained in variables x, y, z. Meanwhile I only needed one for work :). If you turn your phone smoothly their value will be from -1 to 1 and if they're somehow larger it means that the phone made a quick move in space.
At last, I also found a way to emulate the accelerometer along with multi touch on the computer. It turned out to be quite simple too. For now I only checked it on android, anyway download , install on your device, connect it through a cable to the computer and launch your game in unity (sometimes the programs aren't able to find each other, in such case you'll have to reload them both). You will see a preview of what's going on in unity on your phone screen. Now the accelerometer and multi touch are working and we can finally debug everything thoroughly :)

Minuses – for some reason the gyro sensor data isn't transferred.
After creating the first prototype the control was getting out of hand and I decided to calibrate following the next principle: I placed the phone into a comfortable position and chose the sensibility and playing got much easier.
Well, good things tend to have an ending and after testing in various positions it turned out that a lot of bugs appear after phone calibrating in a horizontal position (the character began moving only in one direction after that). After carefully checking the value of variables during performance I found out that in a position like that Input.acceleration.x has equal value  and it doesn’t matter if the screen of the phone is turned up or down. I was trying to find a way out for a while and I found three: to check all possible combinations of the y and z variables too (decided to leave that for later); to substitute accelerometer for gyro sensor (unfortunately gyro sensor functioning on my device is inaccurate and I can’t emulate it on the computer, so this isn’t an option for now too); leave everything as it is. Before choosing option three I decided to check how all of this was fulfilled in games, which were installed on my phone. To be honest, in most of them the developers were to lazy to do even the slightest calibration (now I get why I’m always trying not to choose accelerometer controls). The only elaborated game on my phone was Hungry Shark. What a pleasant surprise that the developers choose option three as well :Р.
Anyway, if during calibration you see the screen like this, hope devs don't mind:

But if you place the phone in a vertical position the round will turn into a mark of exclamation and there is no calibration option now.
Therefore, most developers don’t bother that their game cannot be played while you’re lying on your side.

I'm (7a) not sure that we keep the calibration screen but tested anyway to be sure ;)

See You in a ~week :)

No comments:

Post a Comment