ℹ️ Needs Yop2D version 1262 or newer (the last number of the version, for example 1.0.1262). If your version is older, update Yop2D.
Your phone knows when you tilt or shake it. Yop2D lets you use that without writing code: a character that walks when you tilt, a marble that rolls through a maze, a die that rolls when you shake.
- a project open in the editor;
- a phone or tablet: the sensor is the device's own.
Step 1: read the tilt in a formula
- Open a node that has a formula field, for example “Set a property”.
- In the left panel, open the Tilting the phone group.
- Tap inclinaison.x (left-right) or inclinaison.y (top-bottom of the screen): the formula writes itself.
Step 2: a character that walks when you tilt
- Pick your character, then tap Object behavior in the Inspector.
- Choose a platform character or a top-down character.
- On the Controls line, tap Tilt.
- Set the Sensitivity: gentle, normal or strong.
- For a platform character, add a jump button: tilting does not make it jump.
The “Move by tilting” template shows all this ready to use.
Step 3: a rolling marble
- Create a round object (a marble picture or a colored circle).
- Tap Object behavior, then Realistic physics.
- Tick Marble: rolls when you tilt the phone. Yop2D sets the round shape, the bounce and the gravity for you.
- Add walls: objects with the same behavior, set to Fixed.
- If the marble is too fast or too slow, change the Sensitivity.
The “Marble maze” template has a marble, walls, black holes that restart the level and a green circle to reach.
Step 4: react to a shake
- In the Blueprint, add the “Phone shaken” event.
- Connect it to whatever you want: shuffle, roll a die, knock objects down…
- Shake your phone: it takes two quick, sharp jolts. A simple move is not enough, and one second of rest is enforced between two shakes.
The “Shake to roll the die” template writes a random number, random(1, 6), in a die's text on every shake.
Reset the tilt to zero
The zero is the phone's position when the game starts. To redo it (the player changes posture, new level), add the “Reset the tilt to zero” node.
- The screen does not rotate: when a game uses tilt, the screen stays in its starting position, otherwise it would flip while you tilt.
- Device without a sensor: the value stays at 0 and nothing moves.