Search Unity

InteractionManager/RaycastHit returning wrong GameObject

Discussion in 'VR' started by bplotkin, Aug 21, 2018.

  1. bplotkin

    bplotkin

    Joined:
    Nov 1, 2016
    Posts:
    2
    So I'm using a bit of code from the example of how to check what object has been selected when using the InteractionManager_InteractionSourcePressed method.


    Code (CSharp):
    1.  
    2. private void InteractionManager_InteractionSourcePressed(InteractionSourcePressedEventArgs args)
    3.     {
    4.         hand = args.state.source.handedness;
    5.         InteractionSourceState interactionSourceState = args.state;
    6.  
    7.         if (args.pressType == InteractionSourcePressType.Select)
    8.         {
    9.             if (MenuWindow != null && MenuWindow.activeSelf)
    10.             {
    11.                 SelectMenuItem(0);
    12.                 return;
    13.             }
    14.             var sourcePose = interactionSourceState.sourcePose;
    15.  
    16.             Vector3 sourceGripPosition;
    17.             Quaternion sourceGripRotation;
    18.             bool getPos = sourcePose.TryGetPosition(out sourceGripPosition, InteractionSourceNode.Pointer);
    19.             bool getRot = sourcePose.TryGetRotation(out sourceGripRotation, InteractionSourceNode.Pointer);
    20.             if (getPos && getRot)
    21.             {
    22.                 RaycastHit raycastHit;
    23.                 if (Physics.Raycast(sourceGripPosition, sourceGripRotation * Vector3.forward, out raycastHit, 10))
    24.                 {
    25.                     GameObject go = raycastHit.collider.gameObject;
    26.  
    The problem is that the gameobject being returned is NOT the game object that I selected from the game. What am I doing wrong?
     
  2. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    There are a few things that seems a little off about the raycast. I can see you have vars named Grip, but you are grabbing the pointer position is this intentional?. The forward you are supplying the raycast seems like to will send the ray out in a offset.

    Can you give me a little more insight into what you are seeing?
     
  3. bplotkin

    bplotkin

    Joined:
    Nov 1, 2016
    Posts:
    2
    As stated, this code is what came from Microsoft that I'm trying to use but not having any success. What I am trying to achieve is this:

    Running Windows VR, a user points the controller at an object. I need to know what object the controller was pointed at when the user presses the select/trigger button.

    I believe that the code is attempting to give me that information, but alas, it's wrong - especially after the user starts teleporting around the scene.