0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Meta Quest3 コントローラーを振ったら玉が打つ

Posted at
1 / 2

目的

Unity上でMeta Quest3を用いて、VR空間でコントローラーを振ったらゴーグルから玉が出るようにする!!

実行環境

Unity 2022.3.10f1
Meta Quest3

実装

前提として、「Meta XR All-in-One SDK」をインストールをしてください。

image_01

コントローラーを振ったら玉が出るようにする

➀「MainCamera」を「Delete」してください。
1.png
➁「OVRCameraRig」をドラック&ドロップをしましょう。
「Packages」→「Meta XR Core SDK」→「Prefabs」→「OVRCameraRig」
2.png
➂「OVRControllerPrefab」を「OVRCameraRig」の中の「LeftControllerAnchor」と「RightControllerAnchor」の所にドラック&ドロップをしてください。
「Packages」→「Meta XR Core SDK」→「Prefabs」→「OVRControllerPrefab」
3.png
➃「OVRCameraRig」の中の「LeftControllerAnchor」と「RightControllerAnchor」のをクリックして右側のInspectorをみて、「OVRContollerHelper」のScriptの中の「Controller」で「LeftControllerAnchor」は「LTouch」を選んでください。「RightControllerAnchor」は「RTouch」を選んでください。
4.png
5.png
⑤「OVRCameraRig」に「HandMove」のScriptを作成してください。
「OVRCameraRig」をクリックして右側のInspectorをみて、下の「Add Component」があるのでそこで「HandMove」のScriptを作成してください。6.png
➅「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」

7.png

結果

Unity上で再生して、コントローラーを振ると自分が見ているVRゴーグルから玉が出るようになります。

9.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?