目的
Unity上でMeta Quest3を用いて、VR空間でコントローラーを振ったらゴーグルから玉が出るようにする!!
実行環境
Unity 2022.3.10f1
Meta Quest3
実装
前提として、「Meta XR All-in-One SDK」をインストールをしてください。

コントローラーを振ったら玉が出るようにする
➀「MainCamera」を「Delete」してください。
➁「OVRCameraRig」をドラック&ドロップをしましょう。
「Packages」→「Meta XR Core SDK」→「Prefabs」→「OVRCameraRig」
➂「OVRControllerPrefab」を「OVRCameraRig」の中の「LeftControllerAnchor」と「RightControllerAnchor」の所にドラック&ドロップをしてください。
「Packages」→「Meta XR Core SDK」→「Prefabs」→「OVRControllerPrefab」
➃「OVRCameraRig」の中の「LeftControllerAnchor」と「RightControllerAnchor」のをクリックして右側のInspectorをみて、「OVRContollerHelper」のScriptの中の「Controller」で「LeftControllerAnchor」は「LTouch」を選んでください。「RightControllerAnchor」は「RTouch」を選んでください。
⑤「OVRCameraRig」に「HandMove」のScriptを作成してください。
「OVRCameraRig」をクリックして右側のInspectorをみて、下の「Add Component」があるのでそこで「HandMove」のScriptを作成してください。
➅「HandMove」をダブルクリックをしてVisual Studioを開いてください。
以下のコードがあるのでコピー&ペーストをして保存を忘れずにしてください。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandMove : MonoBehaviour
{
[SerializeField]
Transform leftControllerTransform; // 左手のTransformをInspectorから割り当てる
[SerializeField]
Transform rightControllerTransform; // 右手のTransformをInspectorから割り当てる
[SerializeField]
GameObject CenterEyeAnchor;
[SerializeField]
GameObject bulletPrefab;
public GameObject cameraRig; // CameraRigオブジェクトをInspectorから割り当てる
public float handMovementThreshold = 0.1f; // 手の動きのしきい値
private Vector3 previousLeftHandPosition;
private Vector3 previousRightHandPosition;
private bool isLeftHandMoving;
private bool isRightHandMoving;
private Rigidbody rb;
private void Awake()
{
rb = cameraRig.GetComponent<Rigidbody>();
if (rb == null)
{
rb = cameraRig.AddComponent<Rigidbody>();
rb.useGravity = false; // VR空間なので重力は通常不要です
}
}
private void Start()
{
previousLeftHandPosition = leftControllerTransform.position;
previousRightHandPosition = rightControllerTransform.position;
}
private void Update()
{
Vector3 currentLeftHandPosition = leftControllerTransform.position;
Vector3 currentRightHandPosition = rightControllerTransform.position;
//Vector3 hmdForward = new Vector3(hmdTransform.forward.x, 0, hmdTransform.forward.z).normalized; // HMDの前方向
// 左右の手の前後の動きを判定
isLeftHandMoving = IsHandMoving(currentLeftHandPosition, previousLeftHandPosition);
isRightHandMoving = IsHandMoving(currentRightHandPosition, previousRightHandPosition);
// どちらかの手が動いている場合、移動を実行
if (isLeftHandMoving || isRightHandMoving)
{
Debug.Log("コントローラーを振てる"); // コントローラーを振っている時
GameObject shell = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * 500);
Destroy(shell, 3.0f);
}
if(Input.GetKeyDown(KeyCode.Space))
{
GameObject shell = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * 500);
Destroy(shell, 3.0f);
}
// 手の位置を更新
previousLeftHandPosition = currentLeftHandPosition;
previousRightHandPosition = currentRightHandPosition;
}
private bool IsHandMoving(Vector3 currentHandPosition, Vector3 previousHandPosition)
{
// 手の動きがしきい値より大きいかを判定
return (currentHandPosition - previousHandPosition).magnitude > handMovementThreshold;
}
}
⑦Unity上に戻ってScriptをドラック&ドロップをしましょう。
「OVRCameraRig」をクリックして右側のInspectorをみて、「HandMove」をたくさんドラック&ドロップをします。
「leftControllerTransform」 : 「LeftOVRControllerPrefab」
「rightControllerTransform」 : 「RightOVRControllerPrefab」
「CenterEyeAnchor」 : 「CenterEyeAnchor」
「bulletPrefab」 : 「BulletBlue」
「cameraRig」 : 「OVRCameraRig」
結果
Unity上で再生して、コントローラーを振ると自分が見ているVRゴーグルから玉が出るようになります。