2017-09-30 131 views
0

我想使用Gear VR控制器和Oculus SDK製作Gear VR應用程序。 我得到了GazePointerRing與控制器預製工作。在我的應用程序中有一個可見的分劃板,我可以使用Gear VR控制器移動。它檢測我放置在場景中的立方體。 我現在想要做的是將十字線指向一個立方體並在控制器上按住一個按鈕,這樣立方體就會粘在我的控制器模型上,並可以四處移動,直到我放開按鈕。我一直在OVR Physics Raycaster腳本中搜索如何調用raycast命令並將它輸入到if語句中的按鈕命令中。但是我無法找到一種方法來調用一個對象的raycast命中。 這是OVR物理Raycaster腳本代碼魔環:在Unity中使用Gear VR控制器移動一個立方體

using System.Collections.Generic; 

namespace UnityEngine.EventSystems 
{ 
    /// <summary> 
    /// Simple event system using physics raycasts. 
    /// </summary> 
    [RequireComponent(typeof(OVRCameraRig))] 
    public class OVRPhysicsRaycaster : BaseRaycaster 
    { 
     /// <summary> 
     /// Const to use for clarity when no event mask is set 
     /// </summary> 
     protected const int kNoEventMaskSet = -1; 


    /// <summary> 
    /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used. 
    /// </summary> 
    [SerializeField] 
    public LayerMask m_EventMask = kNoEventMaskSet; 

    protected OVRPhysicsRaycaster() 
    { } 

    public override Camera eventCamera 
    { 
     get 
     { 
      return GetComponent<OVRCameraRig>().leftEyeCamera; 
     } 
    } 

    /// <summary> 
    /// Depth used to determine the order of event processing. 
    /// </summary> 
    public virtual int depth 
    { 
     get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; } 
    } 

    /// <summary> 
    /// Event mask used to determine which objects will receive events. 
    /// </summary> 
    public int finalEventMask 
    { 
     get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; } 
    } 

    /// <summary> 
    /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used. 
    /// </summary> 
    public LayerMask eventMask 
    { 
     get { return m_EventMask; } 
     set { m_EventMask = value; } 
    } 


    /// <summary> 
    /// Perform a raycast using the worldSpaceRay in eventData. 
    /// </summary> 
    /// <param name="eventData"></param> 
    /// <param name="resultAppendList"></param> 
    public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) 
    { 
     // This function is closely based on PhysicsRaycaster.Raycast 

     if (eventCamera == null) 
      return; 

     OVRRayPointerEventData rayPointerEventData = eventData as OVRRayPointerEventData; 
     if (rayPointerEventData == null) 
      return; 

     var ray = rayPointerEventData.worldSpaceRay; 

     float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane; 

     var hits = Physics.RaycastAll(ray, dist, finalEventMask); 

     if (hits.Length > 1) 
      System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance)); 

     if (hits.Length != 0) 
     { 
      for (int b = 0, bmax = hits.Length; b < bmax; ++b) 
      { 
       var result = new RaycastResult 
       { 
        gameObject = hits[b].collider.gameObject, 
        module = this, 
        distance = hits[b].distance, 
        index = resultAppendList.Count, 
        worldPosition = hits[0].point, 
        worldNormal = hits[0].normal, 
       }; 
       resultAppendList.Add(result); 
      } 
     } 
    } 

    /// <summary> 
    /// Perform a Spherecast using the worldSpaceRay in eventData. 
    /// </summary> 
    /// <param name="eventData"></param> 
    /// <param name="resultAppendList"></param> 
    /// <param name="radius">Radius of the sphere</param> 
    public void Spherecast(PointerEventData eventData, List<RaycastResult> resultAppendList, float radius) 
    { 
     if (eventCamera == null) 
      return; 

     OVRRayPointerEventData rayPointerEventData = eventData as OVRRayPointerEventData; 
     if (rayPointerEventData == null) 
      return; 

     var ray = rayPointerEventData.worldSpaceRay; 

     float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane; 

     var hits = Physics.SphereCastAll(ray, radius, dist, finalEventMask); 

     if (hits.Length > 1) 
      System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance)); 

     if (hits.Length != 0) 
     { 
      for (int b = 0, bmax = hits.Length; b < bmax; ++b) 
      { 
       var result = new RaycastResult 
       { 
        gameObject = hits[b].collider.gameObject, 
        module = this, 
        distance = hits[b].distance, 
        index = resultAppendList.Count, 
        worldPosition = hits[0].point, 
        worldNormal = hits[0].normal, 
       }; 
       resultAppendList.Add(result); 
      } 
     } 
    } 
    /// <summary> 
    /// Get screen position of this world position as seen by the event camera of this OVRPhysicsRaycaster 
    /// </summary> 
    /// <param name="worldPosition"></param> 
    /// <returns></returns> 
    public Vector2 GetScreenPos(Vector3 worldPosition) 
    { 
     // In future versions of Uinty RaycastResult will contain screenPosition so this will not be necessary 
     return eventCamera.WorldToScreenPoint(worldPosition); 
    } 
} 
} 

回答

0

先決條件:確保在場景中有OVR經理,它是獨立的,並需要對GearVR控制器(OVRInput類)工作。

我通常的做法是將光線投射在控制器錨位置前進的光線並檢查它是否擊中所需的對象

public class SampleScript : MonoBehaviour 
{ 
    public Transform anchorPos; 
    public GameObject detectionLineObject; // a gameObject with a line renderer 

    private RaycastHit _hitInfo; 
    private LineRenderer _detectionLine; 

    void Start() 
    { 
     GameObject line = Instantiate(detectionLineObject); 
     _detectionLine = line.GetComponent<LineRenderer>(); 
    } 

    void Update() 
    { 
     DetectionManager(); 
    } 

    void DetectionManager() 
    { 
     // check if controller is actually connected 
     if (!OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote) || !OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote)) 
     { 
      return; 
     } 
     // launch a ray from the OVRCameraRig's Anchor Right 
     if (Physics.Raycast(anchorPos.position, anchorPos.forward, out _hitInfo)) 
     { 
      // set our line points 
      _detectionLine.SetPosition(0, anchorPos.position); 
      _detectionLine.SetPosition(1, _hitInfo.point); 

      MyComponent target = _hitInfo.collider.gameObject.GetComponent<MyComponent>(); 

      if (target != null) 
      { 
       // Do you stuff here 
       target.StartInteraction(); 
      } 
     } 
     else 
     { 

      // point our line to infinity 
      _detectionLine.SetPosition(0, anchorPos.position); 
      _detectionLine.SetPosition(1, anchorPos.forward * 500.0f); 
     } 
    } 
} 
+0

我在現場得到了OVRManager。感謝您分享您的光線投射方法,它看起來非常清晰。我想我可以繼續努力,現在就試着去實現它。 –

+0

嗨,關於您對raycast腳本的解決方法只是一些問題。 1.遊戲對象偵測線是您正在瞄準的控制器還是您想與之互動的一個遊戲對象? 2.代碼的用途是什麼: MyComponent target = _hitInfo.collider.gameObject.GetComponent (); MyComponent應該是什麼?例如,通過指向它將一個立方體移動到不同的位置,MyComponent應該是什麼? –

相關問題