Search Unity

Feature Request XR Interactions with Additive Scenes

Discussion in 'XR Interaction Toolkit and Input' started by KingKRoecks, Nov 17, 2020.

  1. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    Right now, in order for a Simple XR Interactable to be found by the XR Interaction System, it needs to exist in the first, primary scene.

    Even if you tell the SceneManager.SetActiveScene, the first scene in the list of loaded scenes is the one where the interaction system attempts to find objects.
    This is easily reproducible at runtime by dragging objects between loaded scenes.

    I'd like to have objects in additive-loaded scenes detectable by the interaction system for the sake of object management. Doing this now, the interaction system cannot detect them.
     
  2. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    231
    The Interaction Manager does not automatically attempt to locate and register interactables or interactors, it is the responsibility of the interactables and interactors themselves to register with the manager. You can set the Interaction Manager property in the Inspector or through scripting, and the interactable/interactor will register itself with that manager. If it is not set, it will first attempt to locate one using FindObjectOfType and then create one if it couldn't find one.

    However, that function can be tricky. During the first frame of entering Play mode, during Awake and OnEnable, objects in the Active Scene will only be able to find activated objects of that type within the Active Scene. This means that the interactors/interactables will only find an Interaction Manager if the manager is in the Active Scene during that time. The order of the Scenes in the Hierarchy shouldn't matter. After that first frame, if you additive load another Scene that contains an interactor/interactable, there shouldn't be any gotchas for finding the Interaction Manager contained in other scenes.

    It should be possible to have objects in additive-loaded scenes be usable with the interaction system as long as you are aware of that limitation around the timing.

    I'll create an issue in our backlog to to improve this finding process so developers avoid getting tripped up on that.
     
  3. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    I think there is still something amiss. I see your point about the XR Interaction Manager and the reference to it on a XR Interaction, but that doesn't really answer why one scene works and the other doesn't.

    The problem appears to occur in this method:

    Code (CSharp):
    1. int CheckCollidersBetweenPoints(Vector3 from, Vector3 to)
    2.         {
    3.             Array.Clear(m_RaycastHits, 0, k_MaxRaycastHits);
    4.  
    5.             // Cast from last point to next point to check if there are hits in between
    6.             if (m_HitDetectionType == HitDetectionType.SphereCast && m_SphereCastRadius > 0f)
    7.             {
    8.                 return Physics.SphereCastNonAlloc(from, m_SphereCastRadius, (to - from).normalized,
    9.                     m_RaycastHits, Vector3.Distance(to, from), raycastMask, raycastTriggerInteraction);
    10.             }
    11.  
    12.             return Physics.RaycastNonAlloc(from, (to - from).normalized,
    13.                 m_RaycastHits, Vector3.Distance(to, from), raycastMask, raycastTriggerInteraction);
    14.         }
    Right now I have 3 scenes in my hierarchy:

    [game]
    [scenery] <- active
    [DontDestroyOnLoad]

    I have my XR Interaction Manager in the DontDestroyOnLoad.

    Previously, my [game] scene was set as active and my interaction was spawning in the [game] scene, but I recently messed around with some code that toggled the ActiveScene and shifted it to [scenery].

    Now that [scenery] is considered active on load, all of the XR Interactions are spawning associated with that scene. No matter what I do, I cannot select the interaction now.

    As soon as I drag it into the [game] scene, the interaction works immediately. If I drag it back into the [scenery] scene, it doesn't work.
     
  4. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    The reference to the XR Interaction Manager is valid, regardless of scene the object is in
     
  5. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    Also, it's not even based on the first scene in the hierarchy, it's based on the first scene that was loaded.

    If I drag [scenery] to show first in the list, the interaction still does not hover.

    with:

    [scenery] <- active
    [game]
    [DontDestroyOnLoad]

    I still need to drag my interaction into [game] that was loaded first in order to get these working.
     
  6. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    I just recreated the scenario with multiple scenes and haven't been able to reproduce. I have a sinking suspicion that this is related to multiplayer and physics scenes
     
  7. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
  8. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    231
    You will have to make sure that any interactable object is in the default physics scene. As you saw in the code you pasted, the
    XRRayInteractor.CheckCollidersBetweenPoints
    method uses the
    Physics
    class for raycasting which will only hit objects in the default physics scene. The section on Multi-scene physics in that blog post does a pretty good job of explaining the limitation along with the Multi-Scene Physics tutorial.

    I'm not familiar with Mirror, but you may need to adjust some settings so that the other scenes are additively loaded without creating local physics scenes.
     
    KingKRoecks likes this.
  9. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    Hey, thanks for the reply! I did end up resolving it.
    I had initially built my multiplayer tech around the MultipleAdditiveScenes demo because of the way I wanted to lay things out.

    With that, came the example code that was adding the Scene with specific `LoadSceneParameters`.

    These parameters would include this:
    localPhysicsMode = LocalPhysicsMode.Physics3D

    Once I removed the LocalPhysicsMode param from the scene params, my interactions started working more reliably.

    I say more reliably, because now I can hover the object and detect. I still get some weird flicker / inconsistent physics, but it's at least detectable in some sense now and I can debug from here.

    That was a really frustrating issue to debug though, holy moly
     
    chris-massie likes this.