Page 1 of 1

Quest 2 Unity VR XR Simple Button Tutorial

Posted: Fri Feb 03, 2023 3:44 am
by darknkreepy3#
Creating A Simple Button in Unity with XR and Quest 2 (for Win10/11 OS)
by Kristoffe Brodeur

02-02-2023 referenced from https://xrbootcamp.com/unity-vr-tutorial-for-beginners/

//------------------------------------------------------------------------------------------------
Downloading And Installing Unity
//------------------------------------------------------------------------------------------------
Unity Hub https://unity3d.com/get-unity/download

Unity Editor 2021.2.8f1
Archive Tab for older versions

Unity Modules {choose these}
Microsoft Visual Studio Community 2019
Android Build Support
Android SDK & NDK Tools
OpenJDK
Windows Build Support (IL2CPP)

//------------------------------------------------------------------------------------------------
Creating A Project
//------------------------------------------------------------------------------------------------
New Project
Editor Version 2021.2.8f1

All templates
3D

Project Name
Simple_XR_Button_V1

Location
* it is a good idea to not always use your [C] drive. OS on [C] and work on [D-Z] drives.
Also maybe you work in more than one game environment, IDE, etc, make subfolders.

D:\00_proj\games\unity\simple_button_xr\

//------------------------------------------------------------------------------------------------
XR Plug-in Management
//------------------------------------------------------------------------------------------------
Unity Editor Top Menu
Edit
Project Settings...
{click} XR Plugin Management
Install XR Plugin Management

// -----
Icon tabs {click} the pc tab
[ pc {monitor} ] | [ android {robot} ]
Plugin-in Providers
select > Open XR

Xr Update required.
Just {click} "I made a Backup, Go Ahead!" if this is a new project.
If it is an older one, decide if you want to update it or back it up first.

{Warning icon} Your project has some settings that are incompatible with OpenXR.
Check to open the project validator.

{Click} the warning icon and the {EDIT} button in the window that pops up
OpenXR window
1. {Click} Interaction profiles [+] icon on the right
choose Oculus Touch Controller Profile

2. Depth Submission Mode choose [Multi Pass] not [Single Pass Instanced]
single pass makes a double-wide texture only half is rendered per eye
multi renders two images for each eye

//-----
Icon tabs {click} the android tab
Plug-in Providers, choose [Oculus]
Look in the Project settings for a new sub-selection under XR Plug-in Management
{click} [Oculus] under it

//------------------------------------------------------------------------------------------------
Configuring VR inputs as actions
//------------------------------------------------------------------------------------------------
Installing the XR interaction Toolkit to use a device to interact with OpenXR in Unity

Unity Editor Top Menu
Window
Package Manager
{click} the [+] button on the top left side of the window
select [Add package from git URL...]
com.unity.xr.interaction.toolkit
{click} [Add]

A warning about an older XR Interaction Toolkit version might exist.
Just {click} "I made a Backup, Go Ahead!" if this is a new project.
If it is an older one, decide if you want to update it or back it up first.

Once it is installed, in Package Manager, choose
[XR Interaction Toolkit] on the left vertical menu

* I used version [2.04] and not 2.20 and I got the proper folder for samples.
The newer version 2.20 didn't really have things all together or the same files.
Check your version {click} the left arrow to open the version listed or versions
under the [XR Interaction Toolkit] menu item.
You can right click an choose to revert to [2.04] for this tutorial.

The info for the package on the right has a SAMPLES arrow area
Choose Default Input Actions and {click} [IMPORT]

Check your Project Tab on the bottom left side of the screen [Project] | [Console]

Go here to see your Assets you just imported

Assets
Samples
XR Interaction Toolkit
2.0.4
Starter Assets
[slider] icon items
XRI Default Continuous Move
XRI Default Continuous Turn
XRI Default Left Controller
XRI Default Right Controller
XRI Default Snap Turn

and
[page and electricity bolt] icon item(s)
XRI Default Input Actions

{click} each [slider] icon item listed in the Starter Assets folder
Look on the top right of the Unity Editor for the [Inspector] tab
{click| [Add to...] for each of these (5) items in the folder

Unity Editor Top Menu
Edit
Project Settings...
Preset Manager
On the right side pane, write Right and Left in the empty areas for
ActionBasedController
Filter
[magnifying icon] Right | XRI Default Right Controller
[magnifying icon] Left | XRI Default Left Controller

//------------------------------------------------------------------------------------------------
Scene Configuration
//------------------------------------------------------------------------------------------------


//-----
XR Origin

Instead of being outside the physical space looking at a screen like a pc, laptop, or handheld...
we are inside with the VR experience in the viewing space usually.

In Unity the [camera] and [sound] are attached (linked positionally) to a gameobject called [XR Origin].

Create an [XR Origin] in Unity

In the Unity Editor choose the top left side tab [Hieracrhy]
1. Right {click} in an empty space and choose
[3D Object]
[Plane]
2. Right {click} in an empty space and choose
[XR]
[XR Origin (Action-based)]

* Differences between
[XR Origin] Just has a head camera
[XR Origin (Action-based)] head camera and controllers

* This automatically imports the [XR Interaction Manager] game object into the scene.
Look in the [Heiracrhy] tab in the:
SampleScene
Directional Light
Plane
XR Interaction Manager
[>]XR Origin

Click on the [XR Interaction Manager] in the [Hierarchy]
view it's properties in the [Inspector] tab on the right side of the Unity Editor

//-----
Input Action Manager

Add a component to the XR Interation Manager to configure input actions in the VR scene.

In the hierarchy, choose the [XR Interaction Manager]
look to the right in the [Inspector] tab

{click} [Add Component]
* these are stacked as you add components to scene items, similar to 3dsmax's approach

type in {Input Action Manager} and select it from the drop down list

in this added component area [Action Assets]
{click} the [+] to add the script we have in our [Starter Scripts] folder [2.04] version

Unity Editor [Project] tab
Assets
Samples
XR Interaction Toolkit
2.0.4
Starter Assets
{electricity zap} icon
XRI Default Input Actions
* you can also drag that icon to the [Element 0] area in the [Inspector] tab
for the
[XR Interaction Manager]
[Input Action Manager]
[Action Assets] component.

The [XR Interaction Manager] in [Hierarchy] should look like this:


Unity Editor
[Hierarchy] tab
XR Interaction Manager
[v] XR Origin
[v] Camera Offset
Main Camera
LeftHand Controller
RightHand Controller

Select the [LeftHand Controller] in that hierarchy and view the [Inspector] tab for it
Make sure the defaults are {checkmarked} which are at least:

[*] XR Controller (Action-based)
Haptic Device Action (nothing for now)
[*] XR Ray Intersector
[*] Line Renderer
[*] XR Interactor Line Visual

Do the same for the [RightHand Controller]

Select the [XR Origin] in the hierarchy listed above as well
make sure:
[Position] X [0] Y [0] Z [0]
[Rotation] X [0] Y [0] Z [0]
[scale] X [1] Y [1] Z [1]

[*] XR Origin Script

Origin Base GameObject XROrigin
Camera Floor Offset Object Camera Offset
Camera GameObject Main Camera (Camera)
Tracking Origin Mode Not Specified
Camera Y Offset 1.1176

//------------------------------------------------------------------------------------------------
Testing Oculus Headset And Hand Controllers In Unity
//------------------------------------------------------------------------------------------------

1. Download and Install The Oculus (META) App for Windows.
https://www.meta.com/quest/setup/

2. Either use
{wired} A long usb3.1 USB-C cable to a USB-C data and power port on your desktop or laptop
{wifi} A WIFI-6 5ghz router to use the AIRLINK connection in the headset to the Windows app [1]
* Do not use the desktop or laptop hosting the Windows App [1] and the headset as well.
Try to keep an ethernet connection direct to the host of the Windows App[1].

//-----
Play Test

Turn on your Quest 2 headset
Connect as
{wired} with usb 3.1x
{wiifi} with the AIRLINK app in the headset to {wifi} to your router, pc directly connected with ethernet

In the Unity Engine
{click} the [PLAY] icon at the top middle area of the Unity Engine

Put on your headset and move your controllers
You should see a the white plane and two red lines for the controller movement.

To Stop the App
On the Controller hold down the menu button and {click} [QUIT APP]
In the Unity Engine
{click} the [STOP] icon at the top middle area of the Unity Engine

//------------------------------------------------------------------------------------------------
Locomotion System
//------------------------------------------------------------------------------------------------
Add a Locomotion System

Unity Editor
In the [Hierarchy] tab
Right {click} an empty area in the [Hierarchy] tab
Choose [XR]
[Locomotion System (Action-based)]

Unity Editor
In the [Hierarchy] tab
{click} [Locomotion System (Action-based)]
Look at the [Inspector] tab

//------------------------------------------------------------------------------------------------
Teleportation Area
//------------------------------------------------------------------------------------------------
This is for the point, {click} and let go of the Quest controllers to sort of checkerboard jump.

Unity Editor
In the [Hierarchy] tab
Right {click} an empty area in the [Hierarchy] tab
Choose [XR]
[Teleportation Area]

* This creates a plane just like the plane we already made, same size.
It makes it hard to realize we did anything, just look at the [Hierarchy] tab.

{click} the new [Teleportation Area]
Look at the [Inspector] tab and see the components:

[*] Plane (Mesh Filter)
[*] Mesh Renderer
[*] Mesh Collider
[*] Teleportation Area
Default-Material (Material)

There are two types of teleportation available in this editor example:

[Teleportation Area] Any area on the plane, 2D surface [X] horizontal and [Y] vertical completely.
Similar to going to a point on an etch-a-sketch instantly.

[Teleportation Anchor] Specific position only where you point your cursor at.
Similar to a specific single point on a map.

* In this example, we will
Disable the new [Transportation Area] game object we just made.
Create a [Transportation Area] component into the stack of the original [Plane] game object we made.

Unity Editor
In the [Hierarchy] tab
{click} the [Teleportation Area]
In the [Inspector] tab
[ ] uncheck (disable) the object, on the top left side of the [Inspector] tab.
It is next to the text area saying "teleportation Area"

Unity Editor
In the [Hierarchy] tab
{click} the [Plane]
In the [Inspector] tab
{click} [Add Component]
Yype "Teleportation Area" and choose it when it shows in the dropdown list


//Create a ground material (green)
Unity Editor
[Project] tab
{click} [+] and choose [Folder]
In the Project pane on the right, press [F2] and rename the folder "Materials"
Double {click} into the [Materials] folder

Right {click} in an empty area of the new Materials Folder pane on the right
Choose [Create]
[Material]

Right {click} [New Material] and rename it to "PlaneColor" for instance.
{click} [PlaneColor] and look at the [Inspector] tab
{click} the white horizontal bar in the Shader's Albedo area.
{click} green on the outer wheel area
{click} a more saturated green in the top right side of the inner area.

back in the [Project] tab
{click} and drag the [PlaneColor] to the plane in the 3D [Scene] tab viewport and let go.

//-----
Play Test

Again, turn on your quest2 headset if off and connect via wired or airlink.

Press the [>] Play icon in the Unity Editor up at the top in the middle

Put on the headset and test

Handset Lines
A red line isn't colliding with the [Plane] game object.
A white line shows collision with the [Plane] game object.

Rotation
Use the joystick on the handset pressing [left] and [right] to control rotation.

//------------------------------------------------------------------------------------------------
Continuous Move
//------------------------------------------------------------------------------------------------

[Modify the Locomotion System] game object.

In the [Hierarchy] tab
{click} [Locomotion System]
In the [Inspector] tab

1. {click} [Add Component]
type "Continuous Move Provider (Action-based)"

2. {click} [Add Component]
type "Continuous Turn Provider (action-based)"

3. In the [Continuous Move Provider] component
[ ] uncheck "right Hand Move Action" [Use Reference]

4. In the [Continuous Turn Provider] component
[ ] uncheck "left Hand Move Action" [Use Reference]

5. [ ] uncheck the [Teleportation Provider] component

6. [ ] uncheck the [Snap Turn Provider] component