controller in steamvr A custom hand model Reference

Types of input in SteamVR, receive input from HTC Vive and taking action with this input. Adding custome hand models to controller and creating your own animation when certain buttons is pressed down.

The simple one uses TrackedController

1
2
3
4
5
6
7
8
9
10
11
private SteamVR_TrackedController device;
void Start(){
device = GetComponent<SteamVR_TrackedController>();
device.TriggerClicked += Trigger;
}
void Trigger(object sender, ClickedEventArgs e){
Debug.Log("Trigger has been pressed.");
}

The second one uses TrackedObject and Controller.Device
This one is complicate, but more flexible.

If you want to get specific information from touchpad input:

1
2
3
4
5
6
7
8
9
10
11
12
private SteamVR_TrackedObject trackedObject;
private SteamVR_Controller.Device deivce;
void Start(){
trackedObject = GetComponent<SteamVR_TrackedObject>();
}
void Update(){
device = SteamVR_Controller.input((int)trackedObject.index);
if(device.GetAxis().x != 0 || device.GetAxis().y != 0)
Debug.Log(device.GetAxis().x + " " + device.GetAxis().y);
}

If you want to get trigger input with the same method:

1
2
3
4
5
void Update(){
device = SteamVR_Controller.Input((int)trackedObject.index);
if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
Debug.Log("Trigger Pressed.");
}

A custom hand model

How to change the controller model with a custom hand model?

Just delete the original hand model under controller(left) and controller(right) in the hierarchy window, and replace it with your own hand model.

How to add animation when controller’s buttons are pressed down?

The most simple way is to add trigger parameters into your hand model’s animator, make several transactions between two status and set the transaction condition with the trigger being set.

controller_hand_animation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class controller_hand_animation : MonoBehaviour {
public GameObject handModel;
private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip;
private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger; //triggerButton Setting
private SteamVR_TrackedObject trackedObject;
private SteamVR_Controller.Device device;
// Use this for initialization
void Start () {
trackedObject = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void FixedUpdate () {
device = SteamVR_Controller.Input((int)trackedObject.index);
if (device.GetPress(triggerButton))
FingerPut();
else FingerUnput();
if (device.GetPress(gripButton))
FistHold();
else FistUnhold();
}
void FistHold() {
handModel.GetComponent<Animator>().ResetTrigger("gripButtonUnpressed");
handModel.GetComponent<Animator>().SetTrigger("gripButtonPressed");
}
void FistUnhold() {
handModel.GetComponent<Animator>().ResetTrigger("gripButtonPressed");
handModel.GetComponent<Animator>().SetTrigger("gripButtonUnpressed");
}
void FingerPut() {
handModel.GetComponent<Animator>().ResetTrigger("triggerButtonUnpressed");
handModel.GetComponent<Animator>().SetTrigger("triggerButtonPressed");
}
void FingerUnput()
{
handModel.GetComponent<Animator>().ResetTrigger("triggerButtonPressed");
handModel.GetComponent<Animator>().SetTrigger("triggerButtonUnpressed");
}
}

Reference

  1. SteamVR: Controller Input in 5 Minutes
  2. Unity Doc: Animation from external sources
  3. Unity Doc: Animation System Overview