//==================================================================================== // // Purpose: Provide ability to use an interactable object when it is being touched // // This script must be attached to a Controller within the [CameraRig] Prefab // // The SteamVR_ControllerEvents and SteamVR_InteractTouch scripts must also be // attached to the Controller // // Press the default 'Trigger' button on the controller to use an object // Released the default 'Trigger' button on the controller to stop using an object // //==================================================================================== using UnityEngine; using System.Collections; public class SteamVR_InteractUse : MonoBehaviour { public bool hideControllerOnUse = false; public float hideControllerDelay = 0f; public event ObjectInteractEventHandler ControllerUseInteractableObject; public event ObjectInteractEventHandler ControllerUnuseInteractableObject; GameObject usingObject = null; SteamVR_InteractTouch interactTouch; SteamVR_TrackedObject trackedController; SteamVR_ControllerActions controllerActions; public virtual void OnControllerUseInteractableObject(ObjectInteractEventArgs e) { if (ControllerUseInteractableObject != null) ControllerUseInteractableObject(this, e); } public virtual void OnControllerUnuseInteractableObject(ObjectInteractEventArgs e) { if (ControllerUnuseInteractableObject != null) ControllerUnuseInteractableObject(this, e); } private void Awake() { if (GetComponent() == null) { Debug.LogError("SteamVR_InteractUse is required to be attached to a SteamVR Controller that has the SteamVR_InteractTouch script attached to it"); return; } interactTouch = GetComponent(); trackedController = GetComponent(); controllerActions = GetComponent(); } private void Start() { if (GetComponent() == null) { Debug.LogError("SteamVR_InteractUse is required to be attached to a SteamVR Controller that has the SteamVR_ControllerEvents script attached to it"); return; } GetComponent().AliasUseOn += new ControllerClickedEventHandler(DoStartUseObject); GetComponent().AliasUseOff += new ControllerClickedEventHandler(DoStopUseObject); } private bool IsObjectUsable(GameObject obj) { return (interactTouch.IsObjectInteractable(obj) && obj.GetComponent().isUsable); } private bool IsObjectHoldOnUse(GameObject obj) { return (obj && obj.GetComponent() && obj.GetComponent().holdButtonToUse); } private int GetObjectUsingState(GameObject obj) { if (obj && obj.GetComponent()) { return obj.GetComponent().UsingState; } return 0; } private void SetObjectUsingState(GameObject obj, int value) { if (obj && obj.GetComponent()) { obj.GetComponent().UsingState = value; } } private void UseInteractedObject(GameObject touchedObject) { if ((usingObject == null || usingObject != touchedObject) && IsObjectUsable(touchedObject)) { usingObject = touchedObject; OnControllerUseInteractableObject(interactTouch.SetControllerInteractEvent(usingObject)); usingObject.GetComponent().StartUsing(this.gameObject); if (hideControllerOnUse) { Invoke("HideController", hideControllerDelay); } usingObject.GetComponent().ToggleHighlight(false); } } private void HideController() { if(usingObject != null) { controllerActions.ToggleControllerModel(false); } } private void UnuseInteractedObject() { if (usingObject != null) { OnControllerUnuseInteractableObject(interactTouch.SetControllerInteractEvent(usingObject)); usingObject.GetComponent().StopUsing(this.gameObject); if (hideControllerOnUse) { controllerActions.ToggleControllerModel(true); } usingObject.GetComponent().ToggleHighlight(false); usingObject = null; } } private void DoStartUseObject(object sender, ControllerClickedEventArgs e) { GameObject touchedObject = interactTouch.GetTouchedObject(); if (touchedObject != null && interactTouch.IsObjectInteractable(touchedObject)) { UseInteractedObject(touchedObject); if (!IsObjectHoldOnUse(usingObject)) { SetObjectUsingState(usingObject, GetObjectUsingState(usingObject) + 1); } } } private void DoStopUseObject(object sender, ControllerClickedEventArgs e) { if (IsObjectHoldOnUse(usingObject) || GetObjectUsingState(usingObject) >= 2) { SetObjectUsingState(usingObject, 0); UnuseInteractedObject(); } } }