Search Unity

[Input System] TrackedDeviceRaycaster.cs 3D occlusion check not working correctly

Discussion in 'Input System' started by alexboost222, Nov 4, 2021.

  1. alexboost222

    alexboost222

    Joined:
    Jul 1, 2018
    Posts:
    11
    Hi there! I'm currently using the Input System package version 1.1.1 with the Unity Editor version 2020.3.15f2. The case: i attempted to setup UI interaction using Unity UI package version 1.0.0 with Input System package.
    Firstly i created a canvas with TrackedDeviceRaycaster.cs component on it: (don't look on "Convert to MRTK Canvas" button, it's from Windows XR package)
    upload_2021-11-5_0-2-13.png
    Then i set up EventSystem with InputSystemUIInputModule.cs component. I also set up some actions to test XR interaction with the UI.
    upload_2021-11-5_0-4-8.png
    And all was good, i tried to place a button on the canvas and to click on it and it just worked. Nice!
    But then i created some 3D stuff with non trigger colliders on my scene with the canvas and event system and realised that sometimes button highlights by controller, but there are several 3D objects on the way from controller to button. As u can see, Check for 3D occlusion parameter of TrackedDeviceRaycaster is true. Then i realised, that one object was right becides the canvas, and then checked sources of TrackedDeviceRaycaster.cs. And finally i found, that there is a mistake.
    **Part of code of TrackedDeviceRaycaster.cs**
    Code (CSharp):
    1. #if UNITY_INPUT_SYSTEM_ENABLE_PHYSICS
    2.             if (m_CheckFor3DOcclusion)
    3.             {
    4.                 var hits = Physics.RaycastAll(ray, hitDistance, m_BlockingMask);
    5.  
    6.                 if (hits.Length > 0 && hits[0].distance < hitDistance)
    7.                 {
    8.                     hitDistance = hits[0].distance;
    9.                 }
    10.             }
    11.             #endif
    The mistake is linked with this interesting fact from here https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html.
    There is a note about Physics.RaycastAll method:
    upload_2021-11-5_0-11-56.png
    I guess the fix would be something like this
    Code (CSharp):
    1. #if UNITY_INPUT_SYSTEM_ENABLE_PHYSICS
    2.            if (m_CheckFor3DOcclusion)
    3.             {
    4.                 var hits = Physics.RaycastAll(ray, hitDistance, m_BlockingMask);
    5.          
    6.                 if (hits.Length > 0)
    7.                 {
    8.                     hitDistance = hits.Select(hit => hit.distance).Min();
    9.                 }
    10.             }
    11.             #endif
    *Edit - PR to the github repo https://github.com/Unity-Technologies/InputSystem/pull/1458
     

    Attached Files:

    Last edited: Nov 4, 2021