2

我正在研究一個Google Cardboard項目,現在我有一個Android的演示,你可以在特殊場景中看到我在UNITY 3D中構建的一切,工作正常&看起來不錯,但是我真正想要的是:UNITY 3D磁鐵腳本

我想按下Google Cardboard磁鐵按鈕時向前走。

我在網上發現了一些腳本,但我不知道如何讓這些腳本在我的UNITY項目中工作。

有人可以幫我進一步嗎?

+0

用戶CaseyB張貼這樣的實用程序,在對象上使用統一的事件系統 - https://github.com/CaseyB/UnityCardboardTrigger/tree/develop 有了這個腳本,事件OnCardboardTrigger()將運行當按下按鈕時,其他腳本貼在同一個對象上 – Agumander

+0

嘿Agumander,謝謝你的回覆! :)如果我把腳本放在我的FirstPersonController上,我應該怎麼做才能讓控制器移動? –

+0

將一個函數放入名爲'OnCardboardTrigger()'的腳本中,並在該函數內部放置移動控件。據我所知,這個函數會觸發每一幀你有按鈕,這樣的運動控制可以像'transform.position + = transform.forward * Time.deltaTime *速度;' – Agumander

回答

2

假設您能夠正確讀取磁鐵輸入。這是我如何做FPS樣式控制器腳本:

  1. 在Unity5中導入資產包標準資產/字符。
  2. 從該包創建一個RigidBodyFPSController.prefab實例。
  3. 刪除它的子對象「MainCamera」
  4. 導入Google cardboard unitypackage
  5. 用CardboardMain.prefab替換您在步驟#3中刪除的「MainCamera」
  6. 更新或修改RigidbodyFirstPersonController.cs GetInput()方法的副本。

GetInput()與谷歌紙板前進後退:

private Vector2 GetInput() 
{ 
    Vector2 input = new Vector2 
    { 
     x = Input.GetAxis("Horizontal"), 
     y = Input.GetAxis("Vertical") 
    }; 

    // If GetAxis are empty, try alternate input methods. 
    if (Math.Abs(input.x) + Math.Abs(input.y) < 2 * float.Epsilon) 
    { 
     if (IsMoving) //IsMoving is the flag for forward movement. This is the bool that would be toggled by a click of the Google cardboard magnet 
     { 
      input = new Vector2(0, 1); // go straight forward by setting positive Vertical 
     } 
    } 
    movementSettings.UpdateDesiredTargetSpeed(input); 
    return input; 
} 

谷歌的SDK只支持的檢測磁鐵 「點擊」。如果你想按住磁鐵前進,我建議使用Unity3D資源商店中的Cardboard Controls+。在GitHub上

+0

我沒有IsMoving標誌。說,它不會在目前的情況下 –

+0

在代碼IsMoving線存在被引用上面有一個代碼註釋。該評論用於解釋該代碼行。在這種情況下,什麼會設置IsMoving的值。如果你在IsMoving中的範圍取決於你,你可以在上面的代碼塊的頂部添加一個'public bool IsMoving;'。 – ShawnFeatherly