Htc Vive can't get axis from touchpad input

Hi,

I am trying to use the htc vive touchpad to control the speed the player is moving at by sliding your thumbs up and down the touchpad.

I have created a walking actionset with a vector2 action named Walk

Then, I bind it to the my left vive controller touchpad’s position

I created an empty object named ActivateWalking and load a script “SteamVR_Activate Action Set On Load” in it. And i load a script named “MovementController” into my Controller(left) under CameraRig under Player


I tried two ways of getting the axis

  1. Call the AddOnChangeListener

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR;
    using Valve.VR;
    using Valve.VR.InteractionSystem;

    public static class ExtensionMethods
    {
    public static float Remap(this float value, float from1, float to1, float from2, float to2)
    {
    return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    }
    }

    public class MovementController : MonoBehaviour
    {
    public SteamVR_Action_Vector2 walkAction;
    public Valve.VR.InteractionSystem.Hand hand;
    public float speed;
    public GameObject player;

     private void OnEnable()
     {
     	if(hand == null)
     		hand = this.GetComponent<Valve.VR.InteractionSystem.Hand>();
    
     	if(walkAction == null)
     	{
     		Debug.LogError("<b>[SteamVR.Interaction]</b> No walk action assigned", this);
     		return;
     	}
    
     	walkAction.AddOnChangeListener(OnWalkActionChange, hand.handType);
     }
    
     private void OnDisable()
     {
     	if (walkAction != null)
     		walkAction.RemoveOnChangeListener(OnWalkActionChange, hand.handType);
     }
    
     private void OnWalkActionChange(SteamVR_Action_Vector2 actionIn, SteamVR_Input_Sources inputSource,Vector2 axis, Vector2 delta)
     {
     	
     	Debug.Log($"Coordinate obtained = {delta.y}");
     	float sf = delta.y;
     	if (Mathf.Clamp(sf, -1, 1) == sf)
     	{
     		sf = sf.Remap(-1, 1, 0, 2);
     		Walk(sf);
     	}
     }
    
     private void Walk(float speedFactor)
     {
     	Quaternion rotation;
    
     	InputDevice hmd = InputDevices.GetDeviceAtXRNode(XRNode.CenterEye);
     	if (hmd.isValid)
     	{
     		if (hmd.TryGetFeatureValue(CommonUsages.centerEyeRotation, out rotation))
     		{
     			Debug.Log("rotation is " + rotation);
     			player.transform.position += rotation * Vector3.forward * speed * speedFactor * Time.deltaTime;
     		}
     	}
     }
    

    }

  2. To use SteamVR_Input.GetVector2 in updates

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR;
    using Valve.VR;
    using Valve.VR.InteractionSystem;

    public static class ExtensionMethods
    {
    public static float Remap(this float value, float from1, float to1, float from2, float to2)
    {
    return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    }
    }

    public class MovementController : MonoBehaviour
    {
    public SteamVR_Action_Vector2 walkAction;
    public Valve.VR.InteractionSystem.Hand hand;
    public float speed;
    public GameObject player;

     private void OnEnable()
     {
     	if(hand == null)
     		hand = this.GetComponent<Valve.VR.InteractionSystem.Hand>();
    
     }
    
    
    
     // Update is called once per frame
     private void Update()
     {
     	Vector2 sf = SteamVR_Input.GetVector2("walking", "Walk", hand.handType);
     	float speedFactor = sf.y;
     	speedFactor = speedFactor.Remap(-1, 1, 0, 2);
    
     	Quaternion rotation;
    
     	InputDevice hmd = InputDevices.GetDeviceAtXRNode(XRNode.CenterEye);
     	if (hmd.isValid)
     	{
     		if(hmd.TryGetFeatureValue(CommonUsages.centerEyeRotation,out rotation))
     		{
     			Debug.Log("rotation is " + rotation);
     			if (Input.GetKey(KeyCode.W))
     				player.transform.position += rotation * Vector3.forward * speed * speedFactor * Time.deltaTime;
     		}
     	}
    
     }
    

    }

They both give me an error of

NullReferenceException: Object reference not set to an instance of an object

May someone tell me how could I fix that?
Thank you.

When I call “AddOnChangeListener”, is there anything that I should put for “Vector2 axis” of the “OnWalkActionChange” function?

When using SteamVRInput in unity it defines and compiles code special for your Actions and Actionsets.

You can then do

SteamVR_Actions.default_Throttle.GetAxis(SteamVR_Input_Sources.Any) //axis
SteamVR_Actions.default_Fire.GetState(SteamVR_Input_Sources.Any) //button

Where default_Fire is the name of the Actionset_Action.
You set the definitions in Window->SteamVR Input and then you have to bind them in the steam UI.