当前位置:网站首页>Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
2022-07-03 07:48:00 【Listen to the rain outside the window】
Unity XR Implement interaction ( Grab , Move rotation , delivery , Shooting )
List of articles
Preface
Unity XR Provides XR Interaction Toolkit The interactive toolkit is directly in Package Manager Install in / Update can , Simple interaction requirements can be realized 0 Code , Just attach the corresponding script to the handle and object . This demo uses the latest 2.1.0 preview edition , If the version is lower, some scripts may not be available , Please update to the latest version as far as possible .
This demonstration uses Pico XR SDK, In the import SDK Then they will carry one by themselves XR Interaction Toolkit Of 0.9 edition , Please be sure to start from Package Manager Update in , Otherwise, the subsequent options will be greatly different .
PS: After updating to the new version , Many scripts are available in new versions Action-based The way , And the old version Device-based, This blog will start with Device-based Explain , About Unity Use of new version input system , The author will focus on in the next article .
One 、 Creation of handle
- Ray Interactor: A handle template object with rays will be created
- Direct Interactor: A handle template object without rays will be created
- XR Origin(VR): Suggest , Will directly create VR The camera + Two ray handle objects , It is more convenient and can be changed directly on this basis in the future .
Two 、 Grab of handle
1. XR DirectController The handle grabs directly
- Interaction Manager: Interaction management class — The key , Usually with XR Origin Created together , Generally, the creation handle will be directly attached , If there is no mount, you need to go Hireachy Mount the panel manually .
- Interaction Layer Mask : On grasping objects XR Grab Interactable The script also has an attribute of the same name , Only objects Layer Mask Only when it matches the handle can it be grabbed .
- Attack Transform : Grab position of handle , At the same time, the object can also set the grab position , When grasping, the grasping position of the handle will be the same as that of the object , You can configure the appropriate location according to the specific needs .
- Colliding body : If not using template configuration , It is easy to forget this component by mounting the script by yourself , Only when there is a collision body in direct grasp can we detect the grasping object and carry out the grasping operation , Rays do not need to collide , Just radiographic testing .
2. XR Ray Interactor Handle ray grab
Handle ray capture generally uses the default settings to attract the hit object onto the handle , There is no need to add colliders , Such as Layer Mask And other properties are consistent with the above , I won't repeat .
3. XR Grab Interactable Grab objects
- Interaction Manager/LayerMask/Attack Transform, Several common attributes are XR DirectController Described in , I won't repeat
- Collider: Must have , Grabbing is achieved by collider detection
- Rigibody: Recommended ownership ,VR The effects of grabbing and throwing of grasping objects in the scene are based on their own rigid bodies .
- Interactable Events: It provides a callback for grabbing a series of events , Such as the callback of grabbing objects , Release the callback of the object , Grab the object and press Trigger Key callback and so on , The subsequent shooting function is based on this .
3、 ... and 、 The handle rocker controls movement and rotation
Use the left joystick to control continuous rotation , Take the continuous movement of the right-hand rocker control as an example
According to the picture above , stay XR Origin Add Locomotion System,Continous Turn Provider And so on , Then configure according to the arrow in the above figure .
- Note that these scripts do not have to be placed in XR Origin On the body , Anywhere, but properties must be configured , Put it in XR Origin Easy to manage .
- Continous It's a script that keeps moving and rotating , Controlled by the rocker of the handle , If you want to rotate or move a distance at a time / angle , It can be used Snap Turn/Move, In the lower version of XR Some scripts may not be supported in the interactive package .
Four 、 Handle ray transmission
1. Basic function version
Basic transmission management class Teleportion Provider You have to add , It is recommended to add them together in XR Origin in
Handle in charge of transmission , The first must be XR Ray Interactor Ray control , Generally speaking, parabola will be used , Or Bessel curve , About its related parameters, we can search the knowledge of Bezier curve ,Sample Frequency Sampling points will directly affect the smoothness of the curve .
Reticle It is the indicator after the ray hits the target , It is very suitable as an indicator of the transmission target point , If there is no suitable indicator, it can be simply demonstrated with a sphere .
In addition to the setting of the ray handle , You also need to add Teleportation Area Script ( If you want to only transmit to a fixed point, you can use Anchor, Use consistent ), Then, when the ray detects the ground that can be transmitted, the ray will turn green , Press by default Trigger Key to achieve transmission .
2. premium
Problems to be improved
Only mount the default script , Sometimes it can't meet the needs to a great extent , For example, the transfer function of the general handle will not always have a curve to select the position , We would like it to have a curvilinear rotation position when pressing a key or pushing the rocker , Transfer when releasing the key or rocker .
The handle responsible for transmission cannot be mounted at the same time Ray Interactor and Direct Interactor in other words , We can't just mount the script to realize that the handle is usually responsible for direct grabbing , When you want to transmit, you can get the ray selection position transmission .
Aiming at problems 1,2 Scene , The author is usually responsible for direct grabbing with the left handle , Press down A Take the ray selection position transmission of the key as an example , Give a solution .
The basic idea
- Create two left handle objects , A mount Direct Interactor Responsible for daily grabbing , A mount Ray Interactor Responsible for transmitting .
- Usual Ray Interactor Scripts will be disabled , Only when you press A Key can be enabled to select the position , When released A The post key code controls the transmission request , Disable the script indicator and so on .
About pressing and lifting the handle keys Down and Up event , If you use an old input system, you can refer to this article https://blog.csdn.net/Q540670228/article/details/123139150, The process of starting and disabling scripts is relatively simple , Only the code that initiates the transmission request when lifting the key is given below .
// The acquisition of the delivery manager script is omitted
public TeleportationProvider provider;
// Handle in charge of transmission XRRayInteractor Script , The acquisition process is omitted
public XRRayInteractor teleportRayInteractor;
private void LeftAXButtonUpHandler()
{
// Ray information
RaycastHit rayInfo;
// Judge whether the target point is effective 1. Whether to hit the target 2. Whether they have Teleportation Components
bool isValid = teleportRayInteractor.TryGetCurrent3DRaycastHit(out rayInfo);
isValid = isValid && rayInfo.collider != null &&
(rayInfo.collider.GetComponent<TeleportationArea>() != null ||
rayInfo.collider.GetComponent<TeleportationAnchor>() != null);
if (isValid)
{
// Create a transmission demand and take the location where the ray hits as the destination , Submitted to the provider Transmission queue in .
TeleportRequest request = new TeleportRequest();
request.destinationPosition = rayInfo.point;
provider.QueueTeleportRequest(request);
}
// Disable related scripts and indicators
teleportRayInteractor.enabled = false;
teleportRayInteractor.GetComponent<XRInteractorLineVisual>().reticle?.SetActive(false);
}
5、 ... and 、 Handle control shooting
stay VR The game often has the function of picking up guns and shooting , The general design difficulties are
- Judge whether the object is grabbed , By which handle
- Whether the handle that grabs this object is pressed Trigger key
stay XR Grab Interactable The script has provided a very intimate event , The trigger conditions are the above two . Use as follows
private XRGrabInteractable grab;
private void Awake()
{
grab = GetComponent<XRGrabInteractable>();
// by activated Add a listening function to the event
grab.activated.AddListener(GrabHandler);
}
private void GrabHandler(ActivateEventArgs a)
{
// When the gun is grabbed and pressed Trigger Callback this method after , Trigger firing method .
Shoot();
}
边栏推荐
- Go language foundation ----- 06 ----- anonymous fields, fields with the same name
- experiment.........
- tp3.2和tp5.0的区别
- Go language foundation ----- 16 ----- goroutine, GPM model
- OSPF experiment
- 【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
- Lucene skip table
- Harmonyos third training notes
- Differences between tp3.2 and tp5.0
- 技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
猜你喜欢
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
Technical dry goods | some thoughts on the future of AI architecture
【LeetCode】2. Valid parentheses · valid parentheses
Lucene skip table
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
PAT甲级 1027 Colors in Mars
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
PAT甲级 1032 Sharing
Go language foundation ----- 16 ----- goroutine, GPM model
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
随机推荐
When did you find out that youth was over
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
Pat class a 1030 travel plan
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
华为交换机配置ssh登录远程管理交换机
go语言-循环语句
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
PHP wechat red packet grabbing algorithm
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
PAT甲级 1032 Sharing
Precautions for opensips and TLS SIP trunk docking
GoLang之结构体
LwIP learning socket (API)
基于RNA的新型癌症疗法介绍
C2 several methods of merging VCF files
An overview of IfM Engage
UA camouflage, get and post in requests carry parameters to obtain JSON format content
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
An article for you to understand - Manchester code
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)