Search Unity

Question Switching scenes disables a certain action events??

Discussion in 'XR Interaction Toolkit and Input' started by Gebray, Dec 2, 2022.

  1. Gebray

    Gebray

    Joined:
    Feb 26, 2021
    Posts:
    10
    Hello,
    I have unity project over a large terrain where the player has two alternatives of movement; continuous move and teleportation utilities. The player can change from one to the other one by toggling(pressing down left controller joystick). I have two scripts doing this I got from the internet and used them with a slight modification.
    The first script is Teleportation controller gives as
    Code (CSharp):
    1.  
    2. public class TeleportationController : MonoBehaviour
    3. {
    4.  
    5.  
    6.  
    7.     private static bool _teleportIsActive = false;
    8.  
    9.  
    10.     public enum ControllerType
    11.     {
    12.         RightHand,
    13.         LeftHand
    14.     }
    15.  
    16.  
    17.     public ControllerType targetController;
    18.  
    19.  
    20.     public InputActionAsset inputAction;
    21.  
    22.  
    23.  
    24.     private XRRayInteractor rayInteractor;
    25.  
    26.  
    27.     public TeleportationProvider teleportationProvider;
    28.  
    29.  
    30.     private InputAction _thumbstickInputAction = null;
    31.  
    32.  
    33.     //Stores Action for Teleport Mode Activate
    34.     private InputAction _teleportActivate = null;
    35.  
    36.     //Stores Action for Teleport Mode Cancel
    37.     private InputAction _teleportCancel;
    38.     private void Awake()
    39.     {
    40.         rayInteractor = GetComponent<XRRayInteractor>();
    41.         rayInteractor.enabled = true;
    42.     }
    43.  
    44.  
    45.     void Start()
    46.     {
    47.         //get the ray interactor
    48.        // rayInteractor = GetComponent<XRRayInteractor>();
    49.  
    50.         //We don't want the rayInteractor to on unless we're using the forward press on the thumbstick so we deactivate it here
    51.         rayInteractor.enabled = false;
    52.  
    53.         //This will find the Action Map of our target controller for Teleport Mode Activate.
    54.         //It will enable it and then subscribe itself to our OnTeleportActivate function
    55.         // Debug.Log("XRI " + targetController.ToString());
    56.          _teleportActivate = inputAction.FindActionMap("XRI " + targetController.ToString() + " Locomotion").FindAction("Teleport Mode Activate");
    57.         //_teleportActivate = teleportActive.action;
    58.           _teleportActivate.Enable();
    59.         _teleportActivate.performed += OnTeleportActivate;
    60.  
    61.         //This will find the Action Map of our target controller for Teleport Mode Cancel.
    62.         //It will enable it and then subscribe itself to our OnTeleportCancel function
    63.         _teleportCancel = inputAction.FindActionMap("XRI " + targetController.ToString() + " Locomotion").FindAction("Teleport Mode Cancel");
    64.  
    65.         _teleportCancel.Enable();
    66.         _teleportCancel.performed += OnTeleportCancel;
    67.  
    68.  
    69.         _thumbstickInputAction = inputAction.FindActionMap("XRI " + targetController.ToString()+ " Locomotion").FindAction("Move");
    70.         _thumbstickInputAction.Enable();
    71.     }
    72.  
    73.  
    74.     private void OnDestroy()
    75.     {
    76.         if (_teleportIsActive)
    77.             _teleportActivate.performed -= OnTeleportActivate;
    78.         if (!_teleportIsActive)
    79.             _teleportCancel.performed -= OnTeleportCancel;
    80.     }
    81.  
    82.  
    83.     //We use the Update function to check for when a teleportation event has occured.
    84.     //The checks needed to ensure a succesful teleport event are
    85.     //-Teleporting is currently active
    86.     //-The ray currently the active one
    87.     //-The Thumbstick isn't being pressed
    88.     //-The rayInteractor is hitting a valid target
    89.     //If those pass, we make a teleportation request to the Teleport Provider
    90.     void Update()
    91.     {
    92.  
    93.         if (!_teleportIsActive)
    94.         {
    95.             return;
    96.         }
    97.         if (!rayInteractor.enabled)
    98.         {
    99.             return;
    100.         }
    101.         if (_thumbstickInputAction.triggered)
    102.         {
    103.             return;
    104.         }
    105.         if (!rayInteractor.TryGetCurrent3DRaycastHit(out RaycastHit raycastHit))
    106.         {
    107.             rayInteractor.enabled = false;
    108.             _teleportIsActive = false;
    109.             return;
    110.         }
    111.         if (raycastHit.collider.gameObject.tag != "Terrain") //it was Teleport
    112.         {
    113.  
    114.             rayInteractor.enabled = false;
    115.             _teleportIsActive = false;
    116.             return;
    117.         }
    118.  
    119.         TeleportRequest teleportRequest = new TeleportRequest()
    120.         {
    121.             destinationPosition = raycastHit.point,
    122.         };
    123.  
    124.         teleportationProvider.QueueTeleportRequest(teleportRequest);
    125.  
    126.         rayInteractor.enabled = false;
    127.         _teleportIsActive = false;
    128.     }
    129.  
    130.     //This is called when our Teleport Mode Activated action map is triggered
    131.     private void OnTeleportActivate(InputAction.CallbackContext context)
    132.     {
    133.         if (!_teleportIsActive && this!=null)
    134.         {
    135.             rayInteractor.enabled = true;
    136.             _teleportIsActive = true;
    137.         }
    138.  
    139.     }
    140.  
    141.     //This is called when our Teleport Mode Cancel action map is triggered
    142.     private void OnTeleportCancel(InputAction.CallbackContext context)
    143.     {
    144.         if (_teleportIsActive && rayInteractor.enabled == true)
    145.         {
    146.             rayInteractor.enabled = false;
    147.             _teleportIsActive = false;
    148.         }
    149.  
    150.     }
    151. }
    This is attached to a ray interactor gameobject.

    The second script is attached to the XR Rig game object and is given as:
    Code (CSharp):
    1. public class ToggleLocomotion : MonoBehaviour
    2. {
    3.     public enum LocomotionType
    4.     {
    5.         Continuous,
    6.         Noncontinous
    7.     }
    8.     public GameObject XRRigPlayer; //the game Object holding the movement scripts
    9.     TeleportationProvider _teleport;
    10.     ActionBasedContinuousMoveProvider _continuousMove;
    11.  
    12.     [Header("Locomotion Type")]
    13.     /// <summary>
    14.     /// Default locomotion to use, 0 = continous move. 1 = non continuous move
    15.     /// </summary>
    16.     [Tooltip("Default locomotion to use, 0 = continous move. 1 = non continuous move")]
    17.     public LocomotionType DefaultLocomotion = LocomotionType.Continuous;
    18.     public GameObject leftRayTeleport;
    19.     public GameObject rightRayTeleport;
    20.  
    21.     public LocomotionType SelectedLocomotion
    22.     {
    23.         get { return selectedLocomotion; }
    24.     }
    25.     LocomotionType selectedLocomotion = LocomotionType.Continuous;
    26.     public InputActionReference LocomotionToggleAction = null;
    27.  
    28.     void Start()
    29.     {
    30.         _teleport = XRRigPlayer.GetComponent<TeleportationProvider>();
    31.         _continuousMove = XRRigPlayer.GetComponent<ActionBasedContinuousMoveProvider>();
    32.         //check in case both of them are active at a time
    33.         if(DefaultLocomotion==LocomotionType.Continuous)
    34.         {
    35.             _teleport.enabled = false;
    36.             _continuousMove.enabled = true;
    37.             leftRayTeleport.SetActive(false);
    38.             rightRayTeleport.SetActive(false);
    39.         }
    40.         else if (DefaultLocomotion == LocomotionType.Noncontinous)
    41.         {
    42.             _teleport.enabled = true;
    43.             _continuousMove.enabled = false;
    44.             leftRayTeleport.SetActive(true);
    45.             rightRayTeleport.SetActive(true);
    46.         }
    47.         ChangeLocomotion(DefaultLocomotion);
    48.  
    49.      
    50.  
    51.  
    52.  
    53.     bool actionToggle = false;
    54.  
    55.  
    56.  
    57.     public virtual void CheckControllerToggleInput()
    58.     {
    59.         // if the LocomotionToggleAction is called
    60.         LocomotionToggle();              
    61.     }
    62.  
    63.     void OnEnable()
    64.     {
    65.         if (LocomotionToggleAction)
    66.         {
    67.             LocomotionToggleAction.action.performed -= OnLocomotionToggle; //unsubscribe first if any
    68.             LocomotionToggleAction.action.Enable();
    69.             LocomotionToggleAction.action.performed += OnLocomotionToggle;
    70.         }
    71.     }
    72.    
    73.     private void OnDestroy()
    74.     {
    75.         if(LocomotionToggleAction)
    76.         {
    77.             LocomotionToggleAction.action.Disable();
    78.             LocomotionToggleAction.action.performed -= OnLocomotionToggle;
    79.             Destroy(this.gameObject);
    80.         }
    81.     }
    82.  
    83.     void OnDisable()
    84.     {
    85.         if (LocomotionToggleAction)
    86.         {
    87.             LocomotionToggleAction.action.Disable();
    88.             LocomotionToggleAction.action.performed -= OnLocomotionToggle;
    89.         }
    90.     }
    91.  
    92.     public void OnLocomotionToggle(InputAction.CallbackContext context)
    93.     {
    94.         actionToggle = true;
    95.        // Debug.Log(SelectedLocomotion + "selected");
    96.         LocomotionToggle();
    97.         actionToggle = false;
    98.     }
    99.  
    100.     public void LocomotionToggle()
    101.     {
    102.  
    103.  
    104.         // Toggle the locomotion
    105.  
    106.        ChangeLocomotion(SelectedLocomotion == LocomotionType.Continuous ? LocomotionType.Noncontinous : LocomotionType.Continuous);
    107.      
    108.     }
    109.  
    110.     public void ChangeLocomotion(LocomotionType locomotionType)
    111.     {
    112.         ChangeLocomotionType(locomotionType);
    113.        // UpdateTeleportStatus();
    114.     }
    115.     public void ChangeLocomotionType(LocomotionType loc)
    116.     {
    117.  
    118.         selectedLocomotion = loc;
    119.  
    120.         // Make sure Smooth Locomotion is available
    121.         if (_continuousMove == null)
    122.         {
    123.             _continuousMove = XRRigPlayer.GetComponent<ActionBasedContinuousMoveProvider>();
    124.         }
    125.  
    126.         if (_teleport == null)
    127.         {
    128.             _teleport = XRRigPlayer.GetComponent<TeleportationProvider>();
    129.         }
    130.  
    131.         toggleTeleport(selectedLocomotion == LocomotionType.Noncontinous);
    132.         toggleContinuousMove(selectedLocomotion == LocomotionType.Continuous);
    133.     }
    134.  
    135.     void toggleContinuousMove(bool enabled)
    136.     {
    137.         if (_continuousMove)
    138.         {
    139.             _continuousMove.enabled = enabled;
    140.         }
    141.     }
    142.     void toggleTeleport(bool enabled)
    143.     {
    144.         if (_teleport)
    145.         {
    146.             _teleport.enabled = enabled;
    147.             leftRayTeleport.SetActive(enabled);
    148.             rightRayTeleport.SetActive(enabled);
    149.         }
    150.     }
    151.     public void UpdateTeleportStatus()
    152.     {
    153.         _teleport.enabled = SelectedLocomotion == LocomotionType.Noncontinous;
    154.     }
    155.      
    156.  
    157. }
    This set up works as expected within single scenes. The problem is when I have two scenes I have to play consecutively. If I end up with the continuous move toggle option in the first scene, the teleportation script always doesn't work in the next scene. if I loaded the next scene while I was in the teleport toggle option in the first scene, It works as expected.

    Can anyone help me to detect the problem? I spent almost two weeks to solve this problem, but failed.
     
    Last edited: Dec 3, 2022