- Home /
How can I code which hand is holding an object in SteamVR.
I have an object that can be grabbed using steamVR at two different locations. The SteamVR Intractable script paired with throwable script and and Steam VR_Skeleton_Poser script ( scripts found here https://github.com/ValveSoftware/steamvr_unity_plugin) allow the interaction to work however I would like to have different interactions based on which hand is holding the object.
I am trying: ///
If this is for the left or right hand public void SelectHand(SteamVR_Input_Sources handType) { MoveInteractSphereBasedOnHand(handType); } public void MoveInteractSphereBasedOnHand(SteamVR_Input_Sources inputSource)
{
if (inputSource == SteamVR_Input_Sources.LeftHand)
{
hand.hoverSphereTransform = leftHoverTransform;
}
if (inputSource == SteamVR_Input_Sources.RightHand)
{
hand.hoverSphereTransform = rightHoverTransform;
}
}
In a script using namespace Valve.VR as a starting point but the public void SelectHand() can not be called elsewhere or invoked so I don't even know how to test the code?
I just want to be able to have the held object interact differently based on where it is being held. Any ideas?
Answer by Strixie13 · Oct 24, 2019 at 03:38 AM
I usually just run a Vector3.Distance check between the raw hand tracking positions. Whichever hand has the shortest distance to the object is the one interacting with it. That could also work for the multiple points on the same object. Just see what point the controller is closest to, then run a different function depending on which one is closer.
This sounds very promising but I am unsure how to do what you say? Could you give me a code example?
By the way, I can grab the object in two locations already but only if the left hand grabs in one point and the right hand picks up in another so your idea could fix that but I also need to be able to move the hands hoverSphere transform because the item I am grabbing extends the reach and thus places further hand controls at the end of the object with each end activating different controls. With that in $$anonymous$$d do you think the Vector3.Distance method mentioned would be able to do this?
$$anonymous$$aybe post some of the code you have and I could get a better idea. Generally speaking what you are trying to do is create a grab system, tweaked to your game.
This is a function I wrote for one of my VR games that returns the nearest object. It takes a hand position and will find the nearest pickable game object (based on a layer mask). It might not actually run, just quickly re-typed some of it to show how it might work in an offline game (originally returns ID's and stuff since its in a networked game)
public GameObject ClosestObject(Vector3 handPOS)
{
Collider[] nearbyObjects = Physics.OverlapSphere(handPOS, 2f);
float shortestDist = $$anonymous$$athf.Infinity;
gameObject closestObject = new GameObject();
foreach (Collider col in nearbyObjects)
{
float dist = Vector3.Distance(col.transform.position, handPOS);
if (dist < shortestDist)
{
shortestDist = dist;
closestObject = col.gameObject;
}
}
return closestObject;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612213830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
VR interactable soft bodies?? 0 Answers
Using Leap Motion Assets in Unity 4 0 Answers
Mirror Leap motion hands 1 Answer