


| マッピング | 説明 |
|---|---|
RawButton.RIndexTrigger | 右コントローラーのインデックストリガー |
RawButton.RThumbstickLeft | 右コントローラーのサムスティックを左に押す |
RawButton.RThumbstickRight | 右コントローラーのサムスティックを右に押す |
Button.One | 右コントローラーのAボタン |
Axis1D.PrimaryHandTrigger | 左コントローラーのハンドトリガー |
Secondaryは右コントローラー、Primaryは左コントローラーです。OVRCameraRigを追加していない場合は、次の手順に従ってください。

ControllerScript.csを実装します。ControllerScriptクラスで、次の変数を追加します。 public Camera sceneCamera;
private Vector3 targetPosition;
private Quaternion targetRotation;
private float step;
sceneCameraは、シーンで使用するカメラを表します。targetPositionはカメラの位置を表します。targetRotationはカメラの回転を表します。stepはCube GameObjectにアニメーションを付けるのに役立ちます。Start()でユーザーの前方に設定するStart()関数で、キューブの初期位置を定義します。 void Start()
{
transform.position = sceneCamera.transform.position + sceneCamera.transform.forward * 3.0f;
}
centerCube()関数を作成し、次の行を追加します。 void centerCube()
{
targetPosition = sceneCamera.transform.position + sceneCamera.transform.forward * 3.0f;
targetRotation = Quaternion.LookRotation(transform.position - sceneCamera.transform.position);
transform.position = Vector3.Lerp(transform.position, targetPosition, step);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, step);
}
centerCube()関数は、Cube GameObjectをスムーズにユーザー前方、ビューポートの中央に配置し、ユーザーのヘッドポーズ(カメラ)に従ってキューブを回転させます。Update()関数で、キューブにアニメーションを付けるためのステップ値を定義します。 void Update()
{
step = 5.0f * Time.deltaTime;
}
OVRInput.RawButton.RIndexTriggerからブーリアン値を受け取ることができます。これはトリガーを単純なボタンとして扱います。この値がtrueの場合、ユーザーは現在そのトリガーを押しています。つまり、centerCube()関数を呼び出してキューブを配置したり回転させたりできます。Update()関数に追加します。if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger)) centerCube();
OVRInput.RawButton.RThumbstickLeft (ユーザーはサムスティックを左に押す)とOVRInput.RawButton.RThumbstickRight (ユーザーはサムスティックを右に押す)が返す浮動小数点値を受け取ります。これらの2つの値のいずれかがtrueの場合は、それに応じてキューブを回転させます。Update()関数に追加します。 if (OVRInput.Get(OVRInput.RawButton.RThumbstickLeft)) transform.Rotate(0, 5.0f * step, 0);
if (OVRInput.Get(OVRInput.RawButton.RThumbstickRight)) transform.Rotate(0, -5.0f * step, 0);
OVRInput.Button.Oneが返すブーリアン値を受け取ります。trueの場合、ユーザーはAボタンをちょうど放したところです。OVRInput.SetControllerVibration(float frequency, float amplitude, Controller controllerMask)として定義されているSetControllerVibration()を呼び出す必要があります。この関数を使う場合は、パラメーターについて以下のことを覚えておいてください。amplitudeの期待値は0以上1以下の値です。amplitudeが大きいほど、振動は強くなります。frequencyを1に設定する必要があります。controllerMaskは、右コントローラーを表すOVRInput.Controller.RTouchか、左コントローラーを表すOVRInput.Controller.LTouchになります。amplitudeとfrequencyの両方を0に設定します。Update()関数に追加してください。 if (OVRInput.GetUp(OVRInput.Button.One))
{
OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
}
OVRInput.Axis1D.PrimaryHandTrigger (ユーザーが手のトリガーを押す)が返す浮動小数点値を受け取ります。0.0fより大きい間は、キューブをOVRInput.GetLocalControllerPosition()によって左コントローラーの位置に置き、OVRInput.GetLocalControllerRotation()によって左コントローラーの回転に従って回転させます。どちらもコントローラーを表すパラメーターを受け付けます。Update()関数に追加し、スクリプトを保存します。 if (OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) > 0.0f)
{
transform.position = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
transform.rotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
}



ControllerScript.csのコード全文です。今後の参考にしてください。using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerScript : MonoBehaviour
{
public Camera sceneCamera;
private Vector3 targetPosition;
private Quaternion targetRotation;
private float step;
// Start is called before the first frame update
void Start()
{
// Set initial cube's position in front of user
transform.position = sceneCamera.transform.position + sceneCamera.transform.forward * 3.0f;
}
// Update is called once per frame
void Update()
{
// Define step value for animation
step = 5.0f * Time.deltaTime;
// While user holds the right index trigger, center the cube and turn it to face user
if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger)) centerCube();
// While thumbstick of right controller is currently pressed to the left
// rotate cube to the left
if (OVRInput.Get(OVRInput.RawButton.RThumbstickLeft)) transform.Rotate(0, 5.0f * step, 0);
// While thumbstick of right controller is currently pressed to the right
// rotate cube to the right
if (OVRInput.Get(OVRInput.RawButton.RThumbstickRight)) transform.Rotate(0, -5.0f * step, 0);
// If user has just released Button A of right controller in this frame
if (OVRInput.GetUp(OVRInput.Button.One))
{
// Play short haptic on right controller
OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
}
// While user holds the left hand trigger
if (OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) > 0.0f)
{
// Assign left controller's position and rotation to cube
transform.position = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
transform.rotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
}
}
void centerCube()
// Places cube smoothly at the center of the user's viewport and rotates it to face the camera
{
targetPosition = sceneCamera.transform.position + sceneCamera.transform.forward * 3.0f;
targetRotation = Quaternion.LookRotation(transform.position - sceneCamera.transform.position);
transform.position = Vector3.Lerp(transform.position, targetPosition, step);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, step);
}
}