- Home /
Steam VR 2.0 - Unity 2018+ Controller tracking & architecture question
I've been working on the items and combat system for my VR RPG/RTS game for a few days now and I've spent a good portion of that time digging through Valve's code in order to understand what it does due to lack of updated documentation. Everything I find is out dated, even most questions I can find are as well.
I've went into the SVR2 Hand script and exposed the renderModelInstance variable by making it public and moving it to the global area in the class. This to me feels like a "hacky" way to get the hand and attach my weapon to it...
public class Halper : MonoBehaviour {
public Interactable triggeringObject;
private Hand[] myHands;
GrabTypes myGrabTypes;
Interactable temp;
// Use this for initialization
void Start ()
{
myGrabTypes = GrabTypes.Trigger;
myHands = FindObjectOfType<Player>().GetComponent<Player>().hands;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
temp = Instantiate(triggeringObject);
myHands[0].AttachObject(temp.gameObject, myGrabTypes, Hand.AttachmentFlags.SnapOnAttach | Hand.AttachmentFlags.ParentToHand, null);
temp.gameObject.transform.SetParent(myHands[0].renderModelInstance.transform);
temp.transform.position = myHands[0].renderModelInstance.transform.position;
}
}
}
If I don't attach it DIRECTLY to this RenderModelInstance it doesn't attach the weapon to my actual hand because when I run the game the SVR2 scripts don't actually move the hands in the hierarchy, they move the instanced ones that get created at run-time so using Hand.AttachObject in order to do what I thought would be exactly like it sounded, it instead attaches it to the hand in the hierarchy that is static....
I have a ViveCameraRig with a Player script and a Play Area script on it as well as the helper script I wrote (the block above). Then in the hierarchy on the LeftHand & RightHand objects childed to the ViveCameraRig I have the Vive Role Setter script, the Hand script, and the Steam VR_Behavior_Pose script attached to each controller.
But the thing that doesn't make much sense to me other than attachObject not doing as expected was the fact I MUST have the SteamVR_Behavior_Pose script disabled on my controllers because if I enable it my controllers have a really large offset and never line up with the controller.... and if I have it removed it just doesn't make a controller. I assume this behavior is because it's plugged into the Hand script via drag and drop, I just thought would be enabled prior to run-time and had me wondering if that was relating to my positioning problems when trying to attach my weapons to the controller.
I'm well aware the common thought process is "Just use v1".
Answer by hexagonius · Dec 01, 2018 at 12:20 PM
Have you read the readme of the plugin on GitHub?
https://github.com/ValveSoftware/steamvr_unity_plugin
For a more extensive example including picking up, throwing objects, and animated hands see the Interaction System example at ?SteamVR/Interaction System/Samples/Interactions_Example
the pickup functionality should give you what you want.