# Part 2 ###### tags: `VR coordinate` **目標: VR手把可以在對的位置生成物件,並顯示其位置** step1: VR controller create obj by GrabPinch at position(0,0,0); step2: get VR controller's position. step3: combine step1 and step2. **問題:** 1. 手把在程式碼的命名要跟系統內建的一樣嗎? ex: GrabPinch要叫GrabPinch嗎? 會搞混嗎? 還是會比較方便? (叫不同的話就是要再調設定,告訴它要找到甚麼string命名。) 2. 可以跑但會出現下列錯誤訊息 ``` There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene. ``` 推測為cameraRig在偵測方面已經用到audio listener來監聽事件發生,自建程式碼再增加則不符合其應用。 錯: cameraRig的確偵測了audio listener但出錯是因為忘記把Main camera刪除。(自己忘記lol) 3. 剛剛在偵測的時候亂刪掉好像是SteamVR_Behavior_Pose.cs的256行,可能之後會有問題。(反正現在只是測試應該還好?) 4. 參考SteamVR內建的throw範例後,發現要抓controller的位置時,可以加入一個rigid body給他抓座標 ![](https://i.imgur.com/cFQvZ8K.png) 像圖中這樣,給定一個rigid在controller下方的model裡面。 ![](https://i.imgur.com/ZZ0kybs.png) 成果圖(一):能夠輸出controller正確位置啦~ ![](https://i.imgur.com/VcwXWR5.png) **有效程式碼:** [偵測trigger被按壓的力度](https://forum.unity.com/threads/vive-controller-trigger-press.567997/) ```c# ///2019/01/ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Valve.VR; public class ShowTriggerValues : MonoBehaviour { public SteamVR_Input_Sources LeftInputSource = SteamVR_Input_Sources.LeftHand; public SteamVR_Input_Sources RightInputSource = SteamVR_Input_Sources.RightHand; private void Update() { Debug.Log("Left Trigger value:" + SteamVR_Actions._default.Squeeze.GetAxis(LeftInputSource).ToString()); Debug.Log("Right Trigger value:" + SteamVR_Actions._default.Squeeze.GetAxis(RightInputSource).ToString()); } } ``` 生成cube物件及位置宣告 ```c# mycube = GameObject.CreatePrimitive(PrimitiveType.Cube); mycube.transform.position = new Vector3(1, 1, 1); ```