University of Phoenix**We aren't endorsed by this school
Course
CSS 240
Subject
Computer Science
Date
Jan 2, 2025
Pages
3
Uploaded by LieutenantStraw13597
using System.Collections;using System.Collections.Generic;using UnityEngine;//Allows us to use the Input System to get values for the thumbstickusing UnityEngine.InputSystem;//Allows us to use the Interaction Toolkit to enable and disable our raysusing UnityEngine.XR.Interaction.Toolkit;public class TeleportationController: MonoBehaviour{//Used to determine current active state of the Teleportation Systemstatic private bool _teleportIsActive = false;//Creates an enum that will determine if we're using the right or left controllerpublic enum ControllerType{RightHand,LeftHand}//Stores the target controller from the editorpublic ControllerType targetController;//References our Input Actions that we are usingpublic InputActionAsset inputAction;//References the rayInteractor to be enabled/disabled laterpublic XRRayInteractor rayInteractor;//References the Teleportation Provider so we can use it to teleport the Player in the event of a succesful teleport callpublic TeleportationProvider teleportationProvider;//Will reference the Thumbstick Input Action when the scene starts upprivate InputAction _thumbstickInputAction;//Stores Action for Teleport Mode Activateprivate InputAction _teleportActivate;//Stores Action for Teleport Mode Cancelprivate InputAction _teleportCancel;void Start(){//We don't want the rayInteractor to on unless we're using the forward press on the thumbstick so we deactivate it hererayInteractor.enabled = false;//This will find the Action Map of our target controller for Teleport Mode Activate.//It will enable it and then subscribe itself to our OnTeleportActivate functionDebug.Log("XRI " + targetController.ToString());_teleportActivate = inputAction.FindActionMap("XRI " + targetController.ToString()).FindAction("Teleport Mode Activate");
_teleportActivate.Enable();_teleportActivate.performed += OnTeleportActivate;//This will find the Action Map of our target controller for Teleport Mode Cancel.//It will enable it and then subscribe itself to our OnTeleportCancel function_teleportCancel = inputAction.FindActionMap("XRI " + targetController.ToString()).FindAction("Teleport Mode Cancel");_teleportCancel.Enable();_teleportCancel.performed += OnTeleportCancel;//We grab this reference so we can use it to tell if the thumbstick is still being pressed_thumbstickInputAction = inputAction.FindActionMap("XRI " + targetController.ToString()).FindAction("Move");_thumbstickInputAction.Enable();}private void OnDestroy(){_teleportActivate.performed -= OnTeleportActivate;_teleportCancel.performed -= OnTeleportCancel;}//We use the Update function to check for when a teleportation event has occured. //The checks needed to ensure a succesful teleport event are//-Teleporting is currently active//-The ray currently the active one//-The Thumbstick isn't being pressed//-The rayInteractor is hitting a valid target//If those pass, we make a teleportation request to the Teleport Providervoid Update(){if (!_teleportIsActive){return;}if (!rayInteractor.enabled){return;}if (_thumbstickInputAction.triggered){return;}if (!rayInteractor.TryGetCurrent3DRaycastHit(out RaycastHit raycastHit)){rayInteractor.enabled = false;_teleportIsActive = false;return;}TeleportRequest teleportRequest = new TeleportRequest(){destinationPosition = raycastHit.point,};
teleportationProvider.QueueTeleportRequest(teleportRequest);rayInteractor.enabled = false;_teleportIsActive = false;}//This is called when our Teleport Mode Activated action map is triggeredprivate void OnTeleportActivate(InputAction.CallbackContext context){if (!_teleportIsActive){rayInteractor.enabled = true;_teleportIsActive = true;}}//This is called when our Teleport Mode Cancel action map is triggeredprivate void OnTeleportCancel(InputAction.CallbackContext context){if (_teleportIsActive && rayInteractor.enabled == true){rayInteractor.enabled = false;_teleportIsActive = false;}}}