The problem I wanted to solve :
- When I swipe the screen in the right direction, the cube starts to rotate on the Y axis,
- and when it reach 30°, the cube y rotation snaps smoothly to 90°. And by smoothly i mean a fast animation to 90° and not abrupt change to it. But this is a second issue, not the most important.
The same for the left direction with negative values.
I tried and combined a lot of codes in vain, read about quaternion and eulers yet couldn't figure a proper solution. I hope you guys could help me with your knwoledge as am new to unity. Thank you very much.
As stated by the user BIELIK in this thread, you can rotate an object via swipe using the following code:
(of course, you need to attach the component where this belongs to the GameObject you want to rotate)
Note that the first if condition is used to ease testing on the Unity editor, so that you don't need to build the app into your device every time you want to test it (in case you are building for phones).
About the snapping feature, you could use some basic logic:
Notice that for the ternary conditions I try to find the singular cases (270 degrees, or 0 degrees), but I compare with a slightly lower/greater number. It is only to ensure numerical precision (avoiding ==), and it won't have any behavioral impact on your app, because snappedAngle can only be 0, 90, 180, and 270.
Notice as well that you can substitute rotation and eulerAngles with localRotation and localEulerAngles respectively, according to your needs.
Finally, about interpolation, there are plenty of threads that explain how to perform it. Check this one. The only thing to take into account is the singular cases because if you interpolate from 310 to 0 (e.g.), the movement will follow the opposite direction. You will need to interpolate between 310 and 360, and afterward assign the yaw value to 0.