利用 Oculus Controller 與場景中的元件互動 with Unity

邦哥不會寫程式
程式裡有蟲
Published in
5 min readMay 27, 2019
Photo by Vladislav Skripchenko on Unsplash

上回談到了如何利用 LocalAvatar 來顯示控制器,這次要單獨加入控制器在場景中,並與其它的元件做互動,開始實作吧。

Import Controller Interaction

下載資源檔:Part1Part2Part3
3 個資源檔下載完成後,解壓縮會得到一個 Unity Package,將這個 Package Import 至 Project 中。

新增 Controller

搜尋 RTrackedRemote,加入至 RightHandAnchor

新增 Controller 指標

新增一個 Laser 空物件,包含了 Sphere Object。

在 Laser Object 新增 OVR Pointer Visualizer (Script)Line Renderer Component。

設定 Line Renderer 屬性如下:

  • Cast Shadows:Off
  • Receive Shadows:Disabled
  • Materials:使用 Unlit/Color
  • Position Width: 0.015 (自由設定,數值太大的話會像麵條一樣粗 XD)
  • Use World Space: Enabled

將 Laser 中 Sphere Object 包含的 Sphere Collider Component 移除。

將 OVRCameraRig 的 TrackingSpace Object 設定至 OVR Pointer Visualizer

將 Laser Object 設定至 OVR Pointer Visualizer Line Pointer

將 Laser 的 Sphere Object 設定至 OVR Pointer Visualizer Gaze Pointer

新增 UI

在場景中新增 Canvas,設定下列屬性:

  • Render Mode:World Space
  • Event Camera:OVRCameraRig 的 CenterEyeAnchor
  • 新增 OVR Raycaster (Script) Component
  • 移除 Graphic Raycaster (Script) Component

在 Canvas 中新增一個 Button 與 Text。

此時會發現場景中多了一個 EventSystem Object,設定下列屬性:

  • 新增 OVR Input Module (Script) Component
  • 設定 OVR Input Module Tracking Space:OVRCameraRig 的 TrackingSpace
  • 移除 Standalone Input Module (Script) Component

接下來要加入一些簡單的程式了,在場景中新增一個空物件,並新增一個 Script Component。

程式內容如下:

在 Button Object 的屬性中將名稱改為 btn_1,並在 OnClick 新增一個事件,將 UIController 與 Button 代入,並選擇剛剛撰寫好的 OnButtonClicked Method。

選擇 UIController Object,將 Text Object 代入。

成果展示

選擇 FileBuild And Run,戴上 VR 設備試一下成果吧。

實際在 VR 設備上看到的畫面只會有一個

發現控制器的方向錯了嗎?把控制器的 Rotation Y 數值設為 180 就可以囉!

參考來源:Easy Controller Selection

--

--