Search Unity

How to rotate an object along all 3 axes with finger swipe?

Discussion in 'AR/VR (XR) Discussion' started by RumataM, Oct 28, 2018.

  1. RumataM

    RumataM

    Joined:
    Nov 6, 2016
    Posts:
    15
    I'm working on a simple AR Vuforia application.
    How can I implement the rotation of an object along all three axes with one finger swipe?

    The code I'm currently using has one bug: the rotation of the object depends on its local axes. For example, if I look at the object from the front, everything works as it should, but if I look at the object from the back side, the finger swipe upwards makes it rotate downwards and vice versa.

    Here is this script:
    Code (CSharp):
    1. public float rotSpeed = 30f;
    2.  
    3. void OnMouseDrag()
    4. {
    5. float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
    6. float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.Deg2Rad;
    7.  
    8. transform.Rotate(Vector3.up, -rotX);
    9. transform.Rotate(Vector3.right, -rotY);
    10. }
    But this is not what I need, how can I rotate the object according to the finger swipe direction regardless of the angle from which I look at it?
    I would be very grateful for help!
     
  2. swanijam

    swanijam

    Joined:
    Nov 14, 2016
    Posts:
    23
    You'll want to rotate the object around the axes relative to the camera, rather than the world or the object.

    transform.rotate(<VuforiaCameraReference or Camera.main>.up, -rotX);
    transform.rotate(<VuforiaCameraReference or Camera.main>.right, -rotY);

    also, though this code ought to work just fine, I would get comfortable using Quaternions as soon as you can. You won't have to convert to radians, can easily avoid Gimbal lock, cutout the conversions from quaternion to euler and then back to quaternion, and have axis to lots of useful rotation functions from Quaternion.
     
unityunity