当前位置:网站首页>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();
}
边栏推荐
- experiment.........
- Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
- Huawei switch console password reset, device initialization, default password
- PHP常用排序算法
- 項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
- Segment read
- EtherCAT state machine transition (ESM)
- Go language foundation ------ 12 ------ JSON
- PHP wechat red packet grabbing algorithm
- 【MySQL 12】MySQL 8.0.18 重新初始化
猜你喜欢
[MySQL 12] MySQL 8.0.18 reinitialization
Harmonyos third training notes
技术干货|百行代码写BERT,昇思MindSpore能力大赏
Leetcode 213: looting II
Go language foundation ----- 08 ----- interface
Go language foundation ----- 13 ----- file
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
【踩坑系列】mysql 修改root密码失败
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
技术干货|AI框架动静态图统一的思考
随机推荐
Pat grade a 1029 median
[at] abc 258G - Triangle 三元組可達-暴力
Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
When did you find out that youth was over
[at] abc 258G - Triangle 三元组可达-暴力
Lucene hnsw merge optimization
OSPF protocol summary
Es writing fragment process
PHP常用排序算法
项目经验分享:基于昇思MindSpore实现手写汉字识别
Huawei switch console password reset, device initialization, default password
什么是定义?什么是声明?它们有何区别?
PHP wechat red packet grabbing algorithm
Redis配置文件
An overview of IfM Engage
Redis batch startup and shutdown script
HarmonyOS第三次培训笔记
Lucene introduces NFA
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
What is definition? What is a statement? What is the difference between them?