snovaのブログ

主にプログラミングやデジタルコンテンツについて書きます。最近はPython, Flutter, VRに興味があります。

UnityでMeta Quest2向けのVRアプリを作った

はじめに

年末にQuest2を購入して、いろんなアプリで遊んでいましたが、自分でも作ってみたくなり、チュートリアルを参考にVRアプリを作成しました。

完成したのはただボールを追いかけるだけのクソゲーです。

開発環境

項目 名称
ハード M1 macbook air
OS macOS Monterey 12.3.1
Unity 2019.4.31f1
エディター vscode

製作手順

基本的には公式チュートリアル通りに進めたら完成しました。

初めてのVRアプリを構築する | Oculus開発者

とは言いつつも、いくつか躓くところがありました。

なお、macOSだとandroid studioが入っていれば基本的に何もせずに実機デバッグできます。

改良してみる

公式チュートリアルだけでは面白くないので、修正を加えてみます。

カメラを360°表示できるようにし、壁の中を歩き回れるようにします。 まず、oculus interglationアセットをUnity Asset Storeから導入。

UnityのWindow > Package Managerでダウンロードとプロジェクトへのインポートをし、Oculus/VR/Prefabs/OVRPlayerControllをシーンへ配置します。

OVRPlayerControllerPositionを(0, 0, 5)に設定します。

OVRCameraRigPositionチュートリアルと同様に(0, 10, -20)に設定します。

そして、デバッグ開始しましたが、以下のエラーにて起動せず。

Assets/PlayerController.cs(6,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'PlayerController'

どうやらPlayerControllerが何かと被っているらしいので、適当にnamespaceを追記しました。 以下はPlayerControllerスクリプト全文です。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace NamePlayerController{
  public class PlayerController : MonoBehaviour
  {
      // Appears in the Inspector view from where you can set the speed
      public float speed;

      // Rigidbody variable to hold the player ball's rigidbody instance
      private Rigidbody rb;

      // Called before the first frame update
      void Start()
      {
          // Assigns the player ball's rigidbody instance to the variable
          rb = GetComponent<Rigidbody>();
      }

      // Called once per frame
      private void Update()
      {
          // The float variables, moveHorizontal and moveVertical, holds the value of the virtual axes, X and Z.

          // It records input from the keyboard.
          float moveHorizontal = Input.GetAxis("Horizontal");
          float moveVertical = Input.GetAxis("Vertical");

          // Vector3 variable, movement, holds 3D positions of the player ball in form of X, Y, and Z axes in the space.
          Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

          // Adds force to the player ball to move around.
          rb.AddForce(movement * speed * Time.deltaTime);
      }
  }
}

そして、デバッグを再開するとうまく実行できました。

おわりに

アセット使えばとても簡単にVRアプリ開発ができます。 UnityやVRは全く扱ってこなかったジャンルですが、このアプリは作るのに半日程度で済みました。

XR系は実務でも使えそうなので、夢が膨らみますね。

参考サイト

Google Play and the Google Play logo are trademarks of Google LLC.