'Unity - Rotate Game Object on Swipe / drag gesture regards the camera pan / rotation
So I have tried many script found online and also try to tweak free asset like lean touch in unity.
But I could not find the best fit for my project.
What I want to achieve is ability to rotate a game object in all axis (x, y, z) regarding the camera position / rotation.
The camera will always focus on the game object and rotate around the game object.
So whenever the camera rotate on specific angle I would like to detect that any drag / swipe gesture (x swipe, or y swipe) on screen will rotate the game object on the specific axis (x, y, z) that Perpendicular to the swipe / drag gesture.
Please advice Thanks
Solution 1:[1]
All you need to do for this is using your cameras orientation and do e.g.
void Update()
{
if(Input.touchCount == 1)
{
var touch = Input.GetTouch(0);
targetTransform.Rotate(cameraTransform.right * touch.deltaPosition.x * multiplier, Space.World);
targetTransform.Rotate(cameraTransform.up * touch.deltaPosition.y * multiplier, Space.World);
}
}
Update
As you said you actually only want one axis so the one with the bigger swipe movement you could so something like e.g.
if(touch.deltaPosition.x > touch.deltaPosition.y)
{
targetTransform.Rotate(cameraTransform.right * touch.deltaPosition.x * multiplier, Space.World);
}
else
{
targetTransform.Rotate(cameraTransform.up * touch.deltaPosition.y * multiplier, Space.World);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
