Search Unity

Question Can't seem to "cancel" a teleport

Discussion in 'XR Interaction Toolkit and Input' started by tleylan, Dec 3, 2022.

  1. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    Teleportation is working (on a VR app). I have changed it to react to the primary left hand controller button. It is enabled with the button press and upon letting go the player is teleported.

    If the player changes their mind however currently the only way to prevent the teleport is to point to a non-teleportable object or to teleport next to where you are currently.

    Is it possible (and/or a good idea) to implement a "cancel" action and if so I can't quite figure out what to do to intercept the teleport action. Cancel seems to be set to the grip but I'm not sure what to set to turn off teleportation as it it was not pressed.

    Any pointers? Thanks.
     
  2. JonathanCel

    JonathanCel

    Joined:
    Feb 17, 2021
    Posts:
    22
    So to cancel, it's a few steps, but it's not so bad:

    1: override XRRayInteractor
    Code (CSharp):
    1. public class XRInteractorOverride : XRRayInteractor
    2. etc
    2: Example, define when to cancel:

    Code (CSharp):
    1.  
    2.        protected bool cancelNextTeleport = false;
    3.         public void Update(){
    4.  
    5.               if ( something ){
    6.                      cancelNextteleport = true;
    7.               }
    8.        }
    9.  
    10. or
    11.        // calculate ground angle or something like that
    12.         protected override void OnHoverEntering(HoverEnterEventArgs args)
    13.         {
    14.                 if ( something ){
    15.                         cancelNextTeleport = true;
    16.                 }
    17.         }

    3: Override OnSelectExiting and apply the isCancelled property in the args.

    Code (CSharp):
    1.  
    2.  
    3.         protected override void OnSelectExiting( SelectExitEventArgs args )
    4.         {
    5.               lastDropTime = Time.time;
    6.  
    7.               if ( cancelNextNextTeleport ){
    8.                      cancelNextNextTeleport = false;
    9.                      args.isCanceled = true;   // only avail on the exit event
    10.              }
    11.        }
    12.  
    You can sort of get away with just returning early from OnSelect* and OnHover* overrides, but it can produce weird glitchy issues further down the line.