Unity's OpenXR+XR Interaction Toolkit realizes the disassembly and assembly of the pistol model

foreword

We have realized the disassembly and assembly of the model on the PC side before, what if we use the VR mode to realize the disassembly of the object? How to use two-handed handles to control objects and disassemble objects? Today we will implement a small VR demo based on OpenXR, XR Interaction Toolkit plug-in.

2. Preparations

We need to prepare the Unity2021 project and environment configuration
. With the previous article: Introduction to the basic configuration of OpenXR+XR Interaction Toolkit for Unity , we will not elaborate on these, and you can review them yourself.

Brief description of the process:
We create a new 3D (URP) project
and then install the XR Interaction ToolKit plug-in through PackageManager.
Configure the Preset of XR Interaction Toolkit
to create a new 3D scene
and import the pistol model

ready to work
insert image description here

3. Hierarchy settings

Let's add a few layers first to facilitate the setting of rays and collision masks later. They are:
Gun layer, used for the pistol body
GunPart layer: used for the disassembly submodule of the pistol
insert image description here
The purpose of setting the layers is to control the pistol at any time The collision between the disassembled submodule and the main body, because the collision of the disassembled submodule and the main body is together when disassembled, we need to control them from colliding, so we need to set ProjectSetting->Physics, set Gun and GunPart no collision
insert image description here

3. Pistol disassembly

1. Blocking of the pistol model

According to the sub-node of the pistol model, we separate the detachable components into nodes, and then add colliders and rigid bodies respectively. As shown in the picture:
I split it into 7 large disassembled sub-blocks, and put the others together with the main model as the gun body.
insert image description here

2. The level setting of the pistol

When we see the picture above, we can notice that when we start to disassemble, the pistol and the sub-modules of the pistol have collisions, and we need to set their levels separately. Let's set the level of each sub-module of the pistol as GunPart, and then set the level of the main body of the pistol as Gun.
In this way, when we disassemble, although the submodule and the main body can interact, they do not interact.

3. Grab of the pistol

We are generally used to hold the gun with the right hand, so when we disassemble the gun, we also set it to hold the gun with the right hand and disassemble the sub-components with the left hand. In OpenXR, if we want to pick up the object, we need to add the component XR Grab Interactable to the object. And set the level of its InteractionLayerMask to Gun. If there are no Gun and GunPart layers in the Interaction Layers, please add them. Note that the InteractionLayers layer here is two different settings from the LayerMask that comes with Unity, which is used by OpenXR. Layers for controlling interactive access to VR controllers.
As shown in the figure below:
insert image description here
At this point, if we create an XR Origin, we can test whether we can pick up the pistol through the camera.

4. RightHandController settings

If we want to use ray grabbing, then we use Ray Interactor. If we want to use hands to grab directly, then we use Direct Interactor components. We have talked about this in previous lessons, so I won’t go into details here. Let's just talk about some parameter settings of RightHandController.

XR Ray Interactor Settings

XR Ray Interactor mainly modifies the two levels, and the others can be left as default.
Interaction LayerMask: Gun: interactive mask, only the rays of the Gun layer can click
Raycast Mask: UI, Gun: ray mask, non-Gun layer, no ray exposure Effect.
insert image description here

Direct Interactor Settings

DIrect Interactor can pick up objects directly by hand, mainly by setting an InteractionLayerMask.
InteractionLayerMask: Gun: The hand can only grab objects in the Gun layer at close range.
insert image description here

5. Sub-module disassembly

As we just said, we use the right hand to hold the gun and the left hand to disassemble the gun. Next, we add XR GrabInteractable components to the sub-modules in turn, and set InteractionLayerMask as the GunPart layer.
insert image description here

6.LeftHandController settings

When we use the left hand to disassemble, we can also choose to use the ray grab to disassemble or directly grab and disassemble. Although both can be used, but because the VR handle has a certain size, if we directly grab the gun with the right hand, the left hand can also use direct grab If disassembled, the VR handles may collide together, or even hit our own hands, so we provide you with several options:
1. The left and right hands are both ray grabbing + direct grabbing, which one uses ray grabbing and which uses direct grabbing, Users can choose by themselves.
2. The right hand picks up the gun by ray grabbing, keeping a certain distance between the handle and the gun, and the left hand disassembles the gun by grabbing directly.

XR Ray Interactor Settings

XR Ray Interactor mainly modifies the two levels, and the others can be left as default.
Interaction LayerMask: GunPart: interactive mask, only the rays of the GunPart layer can click
Raycast Mask: UI, GunPart: ray mask, non-GunPart layer, no ray exposure Effect.
insert image description here

XR Direct Interactor Settings

The XR Direct Interactor assembly is used for left-handed direct grab and takedown firearms.
InteractionLayerMask is set to the GunPart layer, and the control left hand can only operate the sub-module of the gun, and cannot pick up the main body of the gun.
insert image description here

4. Disassemble Demo

After the above steps are completed, we can basically run the test demo.

1. Free disassembly

If we disassemble the gun in a free way, then the above should not be a big problem. I have no problem doing it here. As shown below:
insert image description here

2. Process disassembly

If we disassemble the gun in a streamlined way, such as the first step to disassemble the clip, the second dismantling the gun chamber, the third step, etc., then we need to write scripts to assist these functions, and we can also add the currently disassembled The highlighting effect of the component, I will not show the process script here, the logic is relatively simple, that is, add a collision control of the sub-component, and then monitor whether the currently disassembled object is put down. If you want to do a commercial project, the logic here will be more customized, so we won’t say more. The process effect here is as follows: After the first step is to remove the clip, the barrel buckle is highlighted, waiting for the next step of disassembly .
insert image description here

5. Pistol Assembly

Just now we talked about the disassembly of the pistol. If there is disassembly, there must be assembly. Let’s analyze the basic principle of assembly first:
First, we copy a pistol 2, drag and drop the gun modules to be disassembled as separate game objects and put them on the desktop , and delete other useless ones.
In this way, we have two of each gun subcomponent, and we add the collision and XR Interaction components

Next, let's analyze how to assemble it.
We can pick up the scattered sub-modules with the left hand, and then go to collide with the same module sub-components on the gun body of the right hand to trigger the detection. If the trigger is successful, we think the assembly is successful, and we can hide the scattered sub-modules in the left hand and display them. Submodule of the right-hand body. In this way, we complete a module assembly.
insert image description here
Since the assembly cannot be made into a free mode, there must be a sequential assembly, so we must add additional scripts to assist the assembly function. Such as trigger detection, display and hiding of components, control of assembly process, etc. This can be complicated or simple, so I will only talk about the principle of assembly, and I will not demonstrate the code.

Six. Final

Well, this article is here, I hope it can be helpful to you, please leave a message if you have any questions.

Guess you like

Origin blog.csdn.net/qq563129582/article/details/130396983
Recommended