// raycast are not that important so the update function is sufficient public void Update() { if (SelectionModelInstance != null && IsActivated) { Collider[] hitColliders = Physics.OverlapSphere(SelectionModelInstance.transform.position, MaxSphereDiagnoal / 2); if (hitColliders.Length > 0) { foreach (var collider in hitColliders) { VRIL_Interactable tempObj = collider.transform.gameObject.GetComponent <VRIL_Interactable>(); if (tempObj != null && tempObj.Selection_Selectable && tempObj != SelectionModelInstance && tempObj != MainHand && tempObj != SecondaryHand && !SelectableObjects.Contains(tempObj)) { SelectableObjects.Add(tempObj); } } } else { SelectableObjects.Clear(); } } }
/// <summary> /// Coroutine for single ray cast technique /// </summary> /// <returns>WaitForSeconds()</returns> private IEnumerator ShowRay() { SingleRayCastLineRenderer.enabled = true; while (IsActivated) { Ray ray = new Ray(RegisteredControllers[0].transform.position, RegisteredControllers[0].transform.forward); RaycastHit raycastHit; SingleRayCastLineRenderer.SetPosition(0, ray.origin); // check for obstacle at max distance if (Physics.Raycast(ray, out raycastHit, MaxRayDistance)) { VRIL_Interactable interactable = raycastHit.transform.gameObject.GetComponent <VRIL_Interactable>(); if (interactable != null && interactable.Selection_Selectable) { if (!SelectableObjects.Contains(interactable)) { SelectableObjects.Add(interactable); } } else { SelectableObjects.Clear(); } SingleRayCastLineRenderer.SetPosition(1, raycastHit.point); } else { SelectableObjects.Clear(); SingleRayCastLineRenderer.SetPosition(1, ray.GetPoint(MaxRayDistance)); } yield return(null); } SingleRayCastLineRenderer.enabled = false; }
/// <summary> /// Spindle and Wheel Selection Coroutine /// <para>Only used for the selection of models between controller</para> /// </summary> /// <returns></returns> private IEnumerator SpindleAndWheelSelection() { Vector3 pos = RegisteredControllers[0].transform.position - (RegisteredControllers[0].transform.position - RegisteredControllers[1].transform.position) / 2; Collider[] hitColliders; // set up selection model if (SelectionModelInstance == null) { SelectionModelInstance = Instantiate(SelectionModel, pos, Quaternion.identity); SelectionModelInstance.name = "VRIL_SelectionModel_SpindleAndWheel"; float connectionRayDistance = (RegisteredControllers[0].transform.position - RegisteredControllers[1].transform.position).sqrMagnitude; CurrentSphereDiagonal = connectionRayDistance - connectionRayDistance / DistanceDivider; SelectionModelInstance.transform.localScale = new Vector3(CurrentSphereDiagonal, CurrentSphereDiagonal, CurrentSphereDiagonal); } while (selectionModeActivated) { // set line position LineRenderer.SetPosition(0, RegisteredControllers[0].transform.position); LineRenderer.SetPosition(1, RegisteredControllers[1].transform.position); // set model position pos = RegisteredControllers[0].transform.position - (RegisteredControllers[0].transform.position - RegisteredControllers[1].transform.position) / 2; SelectionModelInstance.transform.position = pos; float connectionRayDistance = (RegisteredControllers[0].transform.position - RegisteredControllers[1].transform.position).sqrMagnitude; // resize based on connectionraydistance if (connectionRayDistance < MaxSphereDiagnoal && connectionRayDistance > MinSphereDiagonal) { CurrentSphereDiagonal = connectionRayDistance - connectionRayDistance / DistanceDivider; SelectionModelInstance.transform.localScale = new Vector3(CurrentSphereDiagonal, CurrentSphereDiagonal, CurrentSphereDiagonal); } // colliders hitColliders = Physics.OverlapSphere(SelectionModelInstance.transform.position, CurrentSphereDiagonal / 2); if (hitColliders.Length > 0) { foreach (var collider in hitColliders) { VRIL_Interactable tempObj = collider.transform.gameObject.GetComponent <VRIL_Interactable>(); if (tempObj != null && tempObj.Selection_Selectable && tempObj != SelectionModelInstance && !SelectableObjects.Contains(tempObj)) { SelectableObjects.Add(tempObj); } } } else { SelectableObjects.Clear(); } yield return(new WaitForSeconds(CoroutineWaitTime)); } SelectionModelInstance?.SetActive(false); yield return(null); }
/// <summary> /// Coroutine for Depthray /// <para>Controller is used with a selection model to select objects</para> /// </summary> /// <returns></returns> private IEnumerator ShowRay() { SingleRayCastLineRenderer.enabled = true; Ray ray = new Ray(RegisteredControllers[0].transform.position, RegisteredControllers[0].transform.forward); Collider[] hitColliders; if (SelectionModelInstance == null) { SelectionModelInstance = Instantiate(SelectionModel, ray.GetPoint(0), Quaternion.identity); SelectionModelInstance.name = "VRIL_SelectionModel_DepthRay"; SelectionModelInstance.transform.localScale = new Vector3(SphereDiagnoal, SphereDiagnoal, SphereDiagnoal); CurrentSphereDiagonal = SphereDiagnoal; } else { SelectionModelInstance.SetActive(true); } while (IsActivated) { ray = new Ray(RegisteredControllers[0].transform.position, RegisteredControllers[0].transform.forward); SingleRayCastLineRenderer.SetPosition(0, ray.origin); SingleRayCastLineRenderer.SetPosition(1, ray.GetPoint(MaxRayDistance)); float distance = (RegisteredControllers[0].transform.position - RegisteredControllers[1].transform.position).sqrMagnitude; float pointValue = distance * DistanceMapping; if (pointValue >= MaxRayDistance) { pointValue = MaxRayDistance; } SelectionModelInstance.transform.position = ray.GetPoint(pointValue); hitColliders = Physics.OverlapSphere(SelectionModelInstance.transform.position, CurrentSphereDiagonal / 2); if (hitColliders.Length > 0) { foreach (var collider in hitColliders) { VRIL_Interactable tempObj = collider.transform.gameObject.GetComponent <VRIL_Interactable>(); if (tempObj != null && tempObj.Selection_Selectable && tempObj != SelectionModelInstance && !SelectableObjects.Contains(tempObj)) { SelectableObjects.Add(tempObj); } } } else { SelectableObjects.Clear(); } yield return(new WaitForSeconds(CoroutineWaitTime)); } SelectionModelInstance.SetActive(false); SingleRayCastLineRenderer.enabled = false; }