- Home /
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.
Don't know if it will help you but when I get an axis with S$$anonymous$$mVRInput I do this.
S$$anonymous$$mVR_Actions.default_Throttle.GetAxis(S$$anonymous$$mVR_Input_Sources.Any)
Where default_Throttle is the name of the actionset and the action.
I don't know why but it works. Thank you so much.
Answer by timobugz3 · Feb 25, 2020 at 09:26 AM
When I call "AddOnChangeListener", is there anything that I should put for "Vector2 axis" of the "OnWalkActionChange" function?
Answer by rh_galaxy · Feb 25, 2020 at 02:46 PM
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.
Thanks. I tried GetAxis and GetState for two actions respectively and they both function. What I still do not understand is why hand.handType is causing problem. I learnt to put it as a input from the Planting Tutorial ai$$anonymous$$g to introduce unity game developer to the new s$$anonymous$$mvr 2.0 interaction system. link text
$$anonymous$$aybe its because I also include UnityEngine.XR class, so it is confuse whether I am calling S$$anonymous$$mVR_Interaction.hand or XR.hand
Your answer
Follow this Question
Related Questions
How to get controller input when using the SteamVR Interaction System 2 Answers
Can Input.GetAxis be used without assigning via the Input Manager? 1 Answer
Input for bluetooth controller? 0 Answers
C# HUD axis button display 0 Answers
How do I check if a SteamVR HMD and controllers are active? 1 Answer