Search code examples
c#unity-game-enginetouchvuforia

Lean Rotate Custom Axis with only one finger


I would like to rotate my AR object on itself, so on a single axis, only with one finger. So i use Lean Touch (the free version) and the Script Lean Rotate Custom Axis. This process works very well with two fingers, but i don't find the natural movement. I just want that when you slide your finger to the right, the object turns to the right, and vice versa.

I have already found that this question has been asked here, and tested the proposed answer but it doesnt work. If someone has already encountered this problem or could have a solution, thank you in advance

void OnMouseDrag()
{
    float rotationX = Input.GetAxis("Mouse X") * rotationSpeed * Mathf.Deg2Rad;
    transform.Rotate(Vector3.down, -rotationX, Space.World);
}

Solution

  • You could try to rather implement actually touch support using Input.GetTouch something like e.g.

    private Vector2 lastPos;
    
    private void Update()
    {
        // Handle a single touch
        if (Input.touchCount == 1)
        {
            var touch = Input.GetTouch(0);
    
            switch(touch.phase)
            {
                case TouchPhase.Began:
                    // store the initial touch position
                    lastPos = touch.position;
                    break;
    
                case TouchPhase.Moved:
                    // get the moved difference and convert it to an angle
                    // using the rotationSpeed as sensibility
                    var rotationX = (touch.position.x - lastPos.x) * rotationSpeed;
                    transform.Rotate(Vector3.up, -rotationX, Space.World);
    
                    lastPos = touch.position;
                    break;
            }
        }
    }
    

    or also since Unity also allows GetMouseButtonDown(0) for the first touch:

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            lastPos = (Input.mousePosition);
        }
        else if (Input.GetMouseButton(0))
        {
            var rotationX = ((Input.mousePosition).x - lastPos.x) * rotationSpeed;
            transform.Rotate(Vector3.up, -rotationX, Space.World);
    
            lastPos = Input.mousePosition;
        }
    }