• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Tilting (Android)

B

brooky1010

Guest
Hello, I have some problems with moving my player sprite with tilting.
The app I made if for phones that stand up (not landscape mode).
I want when they tilt to the left my player rotates to the left and when you tilt to the right my player rotates to the right. I tried several things but none of them work.
And also when I tilt forwards it adds 0.3 motion to my player. Thanks!
 

Roderick

Member
For portrait mode, the function you want is device_get_tilt_x. You'd use it much like how you use an analog stick on a game pad: 0 is neutral, -1 is all the way left, 1 is all the way right.

Unlike analog sticks, there is no option to set a dead zone, so you'll want to add code to compensate, because next to nobody will ever hold their phone exactly upright.

Example:
Code:
tilt = device_get_tilt_x;

if (tilt > 0.05) {hspeed += tilt * 4;}
if (tilt < -0.05) {hspeed -= tilt * 4;}
This makes it so that your object has an acceleration of 0-4 in the direction of the tilt, but ignores tilts less than 4.5 degrees (0.05 * 90 degrees) from neutral (straight up and down).
 
Top