Tracking HTC Vive Controller Motion Direction?

Hi all! I was hoping I could get some guidance on how to accurately track HTC Vive controller directional (up,left,right,down) movement.

The problem:
I’m trying to do some motion activated controls. Pretty simple, the user holds down the trigger and if he or she moves the controller to the right, a UI slider increases in value. If he or she moves the controller to the left, the slider decreases in value. However, the increase and decrease directions are only valid, if I’m facing a certain direction (let’s call it forward or 0 degrees). If I instead face “backwards” (flip around 180 degrees), when the controller is moved to the left, the values increase, and they decrease when the controller is moved to the right (i.e. the everything flips). If i stand at either 90 or 270 degrees, functionality breaks down completely.

What I suspect: When the user spins around, the x-plane does not move with them.

What I Have Done:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(SteamVR_TrackedController))]
public class VRSliderControl : MonoBehaviour {

    public Slider slider;
    public GameObject label;
    public Image knob;

    // Changing this value will change by how much the slider value is incremented
    public float incrementBy = 0.1f;

    private SteamVR_TrackedController device;
    private float oldDevicePosX;

    // Changing this value will cause the slider knob to "move faster"
    private float posIncrement = 0.01f;

   
    private float thresholdValue = 0.01f;


	// Use this for initialization
	void Start ()
    {
        PlayerPrefs.SetFloat("MoveSpeed", 0f);

        // Capture Trigger Held Event
        oldDevicePosX = transform.position.x;
        device = GetComponent<SteamVR_TrackedController>();

       
        device.TriggerHeld += OnTrigger;
	}
	
	void OnTrigger(object sender, ClickedEventArgs e)
    {
        
        
        // Increase slider value if user moves controller to the right
        if(transform.position.x > oldDevicePosX && Mathf.Abs(transform.position.x - oldDevicePosX) > thresholdValue)
        {
            oldDevicePosX = transform.position.x;
            // time.deltatime here smooths the slider handle movement
            knob.transform.position = new Vector3(knob.transform.position.x + posIncrement * Time.deltaTime, 
                                                  knob.transform.position.y, 
                                                  knob.transform.position.z);
            slider.value += incrementBy;
            SteamVR_Controller.Input((int)device.controllerIndex).TriggerHapticPulse(1000);
        }
        // Decrease slider values if user moves controller to the left
        else if (transform.position.x < oldDevicePosX && Mathf.Abs(transform.position.x - oldDevicePosX) > thresholdValue)
        {
            oldDevicePosX = transform.position.x;
            knob.transform.position = new Vector3(knob.transform.position.x - posIncrement * Time.deltaTime, 
                                                  knob.transform.position.y, 
                                                  knob.transform.position.z);
            slider.value -= incrementBy;
            SteamVR_Controller.Input((int)device.controllerIndex).TriggerHapticPulse(1000);
        }

        // Update the slider label so that the user knows what the slider value is
        label.GetComponent<LabelSettings>().SetLabelText(slider.value.ToString("F1"));
        PlayerPrefs.SetFloat("MoveSpeed", slider.value);

    }
}

The Question:
Is there a way to get the X-plane to move with the user so that their look direction is always the forward direction / is there just a better more robust way of doing something like this?

Thanks for the help! It’s much appreciated!

Your UI virtual screen I assume is what the motion should be relative to correct? What you need then is Transform.InverseTransformDirection( yourControllersVelocity );

This turns your controller’s velocity to the screen’s local coordinates. So no matter what orientation the screen is, it will always have the same relative coordinates on your controller. I.e. move along the screen increases the x, move up along the screen increases the y, etc.