Jump to content
Happy Holidays! Limited Staff Responses: 1/20 - 1/31 ×

Touchpad Swipe


Codest
 Share

Recommended Posts

  • 4 weeks later...

Hi,

Following sample should do the work.

Also it's cross platform.

using HTC.UnityPlugin.Vive;using UnityEngine;public class SwipeSample : MonoBehaviour, IViveRoleComponent{    [serializeField]    private ViveRoleProperty m_viveRole = ViveRoleProperty.New(HandRole.RightHand);    [serializeField]    private float m_swipeThreshold = 0.75f;    private Vector2 m_downAxis;    public ViveRoleProperty viveRole { get { return m_viveRole; } }    private void OnEnable()    {        ViveInput.AddListener(m_viveRole, ControllerButton.PadTouch, ButtonEventType.Down, OnPadDown);        ViveInput.AddListener(m_viveRole, ControllerButton.PadTouch, ButtonEventType.Up, OnPadUp);    }    private void OnDisable()    {        ViveInput.RemoveListener(m_viveRole, ControllerButton.PadTouch, ButtonEventType.Down, OnPadDown);        ViveInput.RemoveListener(m_viveRole, ControllerButton.PadTouch, ButtonEventType.Up, OnPadUp);    }    private void OnPadDown()    {        m_downAxis = ViveInput.GetPadAxis(m_viveRole);    }    private void OnPadUp()    {        // get axis from last frame        var upAxis = ViveInput.GetPadAxis(m_viveRole, true);        var swipeVector = upAxis - m_downAxis;        // skip if swipe distance is too short        if (swipeVector.sqrMagnitude < m_swipeThreshold * m_swipeThreshold) { return; }        // calculate direction        var upScore = Vector2.Dot(swipeVector, Vector2.up);        var downScore = Vector2.Dot(swipeVector, Vector2.down);        var leftScore = Vector2.Dot(swipeVector, Vector2.left);        var rightScore = Vector2.Dot(swipeVector, Vector2.right);        var maxScore = Mathf.Max(upScore, downScore, leftScore, rightScore);        if (maxScore == upScore)        {            Debug.Log("Swipe Up");        }        else if (maxScore == downScore)        {            Debug.Log("Swipe Down");        }        else if (maxScore == leftScore)        {            Debug.Log("Swipe left");        }        else if (maxScore == rightScore)        {            Debug.Log("Swipe Right");        }    }}

No sure we are going to add this into API.

Is it suitable for controller with joystick? (Oculus Touch, Vive Cosmos Controller)

We have to consider what is the best threshold?

How many directions should we use? 8 or 4 directions?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...