Use unity and steamVR to complete scene roaming (3) Use VRTK to achieve interaction with Vie controller

Interactable object scripts are added to any game object that needs to be interacted with (such as a controller).
 
The available script parameters are as follows
 

Touch  Interactions

Highlight On Touch : If checked, this object will be highlighted when the controller touches it.
Touch Highlight Color: The color of the highlight when the object is touched. Colors can be overridden by any global set color (eg InteractTouch scripts).
· Rumble On Touch : Vibration feedback is triggered when the controller touches the object, x indicates the duration, and y indicates the vibration intensity. (x and y will be replaced by custom editors in the future).
 

Grab  Interactions

Is Grabbale: Determines whether the object can be grasped.
Is Droppable: Determines whether the object can be dropped after being grabbed by the controller. If unchecked, once an item is picked up with the controller buttons, it cannot be put down. But if the gripping mechanism is a joint (Fixed_Joint), when the object is stressed enough, the joint breaks and the item is put down. So it's better to use the grip mechanism of the controller child (Child_Of_Controller) to prevent this.
Hold Button To Grab : If checked, the controller needs to keep holding down the button set to Grab to maintain the Grab state. If unchecked, the grab button toggles the grab behavior by pressing once to grab, and once to release.
· On Grab Collision Delay: The amount of time to delay the effect of collision when the object is first grabbed. This is useful to prevent the game object from getting stuck on another object while it is being grabbed.
Grab Snap Type: This sets the alignment of the object when it is grabbed
     o Simple_Snap : place the center of the grabbed object on the attachment point of the controller (default is the tip of the controller)
     o Rotation_Snap: Adjust the grabbed object to a specified rotation, provided by the Snap To Rotation variable of the Vector3 type.
     o Precision_Snap : Instead of snapping the object's position to the controller, pick up the object with the point where the controller touches the object (like picking things up with your hands in real life).
     o Handle_Snap:  Allows an empty GameObject to be used as a reference snap point as a child of an interactable object. When grabbing, the rotation and position of the empty GameObject is used to position the grabbed interactable object to the controller.
Snap To Rotation: A Vector3 variable representing the Euler angle, which determines the rotation of the object relative to the controller when aligning. Useful when picking up a gun or sword, the relative rotation of the controller is important for ease of use.
Snap To Position: A Vector3 variable that determines the relative position of the object when snapping to the controller.
Snap Hanlde: A Transform provided by an empty GameObject, which must be a child of the grabbed item and used as an anchor point for the rotation and position of the grabbed item relative to the grabbing controller.
· Rumble On Grab:控制器抓取对象时触发振动反馈,x表明时长,y表明震动强度。(x和y将来会被自定义编辑器代替)。


Grab Mechanics 抓握机制

· Grab Attach Type:这决定了抓取的物品在被抓取时怎样被添加到控制器上。
     o Fixed Joint(固定关节):用一个固定的关节把对象附加到控制器上,意味着附加对象以完全1:1的方式追踪控制器的位置和旋转。
     o Spring Joint(弹性关节):用一个弹性关节把对象添加到控制器上,意味着物体和控制器之间有一些弹性,弹力驱动物体。在想要牵引一个物体而不是直接把物体对齐到控制器上是很有效的。就感觉物体有运动阻力一般。
     o Track Object (追踪对象):不是通过关节来把对象附加到控制器上,而是确保对象追踪控制器的方向,这适用于铰链式关节。
     o Child Of Controller (控制器子物体):简单地让对象成为抓取控制器的子物体,所以自然地追踪了控制器运动位置。
 
· Detach Threshold(分离阈值):当对象从被抓取的控制器分离时的力值。如果控制器试着施加一个高于这个阈值的力到对象上(从另一个对象上拉它或者推它到另一个对象),然后保持对象到抓取控制器的关节就将断开并且对象将不能再被抓起了。这在Tracked Object抓取机制下也有效,只是这个 值确定断开抓取钱控制器可以离对象多远。
· Spring Joint Strength(弹簧关节力):弹簧力保持对象到控制器。小数值将意味着弹簧很松并且对象需要一个更大的力去移动,大数值就意味着弹簧很紧,小点的力就可以移动它。
· Spring Joint Damper(弹簧关节阻尼):弹簧的阻尼量在使用弹簧关节抓取机制下有效。在移动被关节的交互对象时,值越高震动效果越小。
· Throw Muliplier(投掷倍数):值用来乘以给定对象被扔出时的速度值。这还可以被用来连同Interact Grab脚本的Throw Multiplier来让对象被扔的比平常更远(或者输入的值小于1就扔的很近)。


Use Interactions 使用交互

· Is Usable(是否可用):决定对象是否可以被使用
· Hold  Button To Use:如果勾选,控制器上的使用(use)按钮需要被持续按下来保持使用。如果没有勾选,use按钮用一次按钮按下来启动使用和再一次按下来停止使用。
· Pointer Avtivates Use Action:如果勾选,当一个世界光束指针(由控制器投射)碰到了交互对象,如果对象的Hold Button To Use没有勾选的话,当指针在对象上的时候就会运行对象的StartUsing方法,指针移开失效就会运行StopUsing方法。这个选项在被勾选以后,为了防止在用一个指针来使用对象时发生不必要的传送,就不会抛出目标点设置( Destination Set )事件。
· Rumble On Use控制器触碰对象时触发振动反馈,x表明时长,y表明震动强度。(x和y将来会被自定义编辑器代替)。


下面的事件是被发送的:
· InteractableObjectTouched:当另一个对象碰到当前对象时发出事件。
· InteractableObjectUntouched: 当其他对象停止触碰当前对象时发出事件。
· InteractableObjectGrabbed:当其他对象(如控制器)抓握当前对象时发出事件。
· InteractableObjectUngrabbed:当其他对象停止抓握当前对象时发出事件。
· InteractableObjectUsed:当其他对象(控制器)使用当前对象时发出事件。
· InteractableObjectUnused:当其他对象停止使用当前对象时发出事件。


发出的事件有效信息载体包涵:
· interactingObject:初始化交互的对象(如控制器)。
 
脚本基本是提供一个简单的机制来区分游戏世界中的对象是否能被抓取或者使用,最好是把这个脚本看做是一个基类来派生出功能更丰富的脚本。
 
VRTK_InteractableObject 的例子可以看SteamVR_unity_Toolkit/Examples/005_Controller_BasicObjectGrabbing.场景。这个场景还在控制器上使用了VRTK_InteractTouch和VRTK_InteractGrab 脚本,来展示一个交互对象怎样被抓取和对齐到控制器以及在游戏世界中扔出。


另外一个例子在场景SteamVR_Unity_Toolkit/Examples/013_Controller_UsingAndGrabbingMultipleObjects。这个场景展示了更多对象,可以被按住按钮抓取或者切换按钮点击来抓取的对象,还有对象通过切换它们的Using 状态来展示多种物品可同时被开启。

 

 


触摸交互对象(VRTK_InteractTouch)


 

触摸交互脚本被添加到[CameraRig] 预制里的控制器对象上。
 
可用参数如下:
· Hide Controller On Touch: 当有效触碰发生时隐藏控制器模型
· Hide Controller Delay:触摸隐藏控制器之前的等待秒数。
· Global Touch Highlight Color 如果交互对象被触碰就能高亮显示,没有设置本地颜色的话就使用全局颜色。


发出事件如下:
· ControllerTouchInteractableObject:当有效对象被碰到时发出
· ControllerUntouchInteractableObject: 当一个有效对象不再被触碰时发出
 
发出事件有效载体信息包含:
· controllerIndex(控制器序号):正在交互的控制器的序号
· Target(目标): 控制器正在交互的对象的GameObject
 
VRTK_InteractTouch 的例子参见SteamVR_Unity_Toolkit/Examples/005_Controller_BasicObjectGrabbing场景。场景演示了添加了 VRTK_InteractableObject 脚本的对象被控制器触碰时能够高亮显示交互对象的能力。
 
 

抓取交互对象(VRTK_InteractGrab)


 

交互抓取脚本被添加到[CameraRig] 预制里的控制器对象上,并且需要添加VRTK_ControllerEvents脚本用来监听抓取和释放交互游戏对象的控制器按钮事件。它监听AliasGrabOn和AliasGrabOff 事件来确定对象合适应该被抓取和释放。
 
控制器对象也需要添加VRTK_InteractTouch 脚本用来确定何时一个可交互对象被触摸到。只有有效的被触摸对象才能被抓取。
 
如果控制器触碰到一个包含VRTK_InteractableObject脚本并且isGrabbable 标志位设置为真的游戏对象,该对象就可以被抓取。
 
如果有效可交互对象是可抓取的,按下控制器上所设置的抓取按钮(默认是Trigger按钮)将抓取和对齐对象到控制器上,并且直到抓取按钮松开才会被释放。
 
当控制器抓取按钮松开时,如果可交互对象是可抓取的,它将会被按一定速率沿着控制器松开时的方向进行推动,这就模拟了对象投掷。
 
可交互对象需要碰撞体用来激活触发和一个刚体用来拾取它们并在游戏世界中四处移动它们。


可用脚本参数如下:
· ************************************************************
· Controller Attach Point控制器模型上用来吸附抓取的对象的刚体点(默认为tip)
· Hide Controller On Grab当有效抓取发生时隐藏控制器模型。
· Hide Controller Delay在抓取时隐藏控制器之前的等待时间秒数。
· Grab Precognition(预先抓取): 在抓取按钮按下时和控制器正在抓取某个东西时之间时间量。例如,如果一个对象下落过快,因为人的反应时间可能很难及时按下抓取按钮来抓住对象。这个值越高意味着抓取键可以越提前在控制器碰到对象以及碰撞发生前按下,如果抓取键仍然被按下(如果抓取对象需要持续按键抓取的话),则抓取动作就会成功。
· Throw Multiplier(投掷乘数):用来乘以对象被扔出时速度的量。在放大 CameraRig 来模拟能够把物体扔的更远时是很有用的。
· Create Rigid Body When Not Touching如果勾选,当抓取按钮按下时,如果控制器没有碰到一个可交互的对象,那么添加到控制器上的刚体就允许控制器去推动周围其他的刚体对象。


发出事件如下:
· ControllerGrabInteractableObject: 当有效对象被抓取时发出
· ControllerUngrabInteractableObject: 当有效对象从抓取到放开时发出
 
发出事件有效载体信息包含:
· controllerIndex: 正在交互的控制器序号
· target: 控制器正在交互的对象的GameObject


VRTK_InteractGrab的例子可以参见SteamVR_Unity_Toolkit/Examples/005_Controller/BasicObjectGrabbing场景。场景演示了抓取挂有VRTK_InteractableObject 脚本的可交互对象。对象可拾起可抛出。
 
更复杂一点的例子可以参见场景SteamVR_Unity_Toolkit/Examples/013_Controller_UsingAndGrabbingMultipleObjects ,这里演示了每个控制器都能单独抓取和使用对象并且对象也可以被同时切换它们的使用状态。SteamVR_Unity_Toolkit/Examples/014_Controller_SnappingObjectsOnGrab场景演示了不同的对齐抓取的对象到控制器上的机制。
 
 
 

使用可交互对象(VRTK_InteractUse)


 

交互使用脚本被添加到[CameraRig] 预制里的控制器对象上,并且需要添加VRTK_ControllerEvents脚本用来监听使用和停止使用交互游戏对象的控制器按钮事件。它监听AliasUseOn 和AliasUseOff 事件来确定对象合适应该被使用和停止使用。
 
控制器对象也需要添加VRTK_InteractTouch脚本来决定何时可交互对象被触碰。只有有效的触碰对象才能被使用。
 
如果控制器触碰到一个包含VRTK_InteractableObject 脚本且isUsable标志位设置为真,这个对象就能被使用。


可用脚本参数如下:
· Hide Controller On Use:当有效使用动作开始时隐藏控制器模型
· Hide Controller Delay:在使用之前,隐藏控制器的等待时间秒数
 
发出事件如下:
· ControllerUseInteractableObject: 当一个有效对象开始被使用时发出
· ControllerUnuseInteractableObject: 当一个有效对象停止被使用时发出
 
发出的事件的有效载体信息包含:
· controllerIndex: 正在交互的控制器序号
· target: 控制器正在交互的对象的GameObject
 
例子参见SteamVR_Unity_Toolkit/Examples/006_Controller_UsingADoor场景,模拟打开一扇门和关上。地上也有一个立方体可以被抓取来展示可交互对象怎么被使用或者抓取。
 
另一个例子可参见SteamVR_Unity_Toolkit/Examples/008_Controller_UsingAGrabbedObject场景,它展示了对象可以用一个按钮抓取并用另一个按钮使用。
 
 

自动抓取可交互对象(VRTK_ObjectAutoGrab)


 

通过把VRTK_ObjectAutoGrab脚本加到控制器上并指定应该默认抓取的对象,来让特定控制器能自动抓取可交互对象。
 
自动抓取对象脚本添加到[CameraRig] 预制里的控制器对象上,控制器对象还需要添加VRTK_InteractGrab 脚本
 
可用脚本参数如下:
· Object To Grab(抓取的对象)在游戏开始时被控制器抓取的游戏对象(在场景或者预制里)。
· Clone Grabbed Object(复制被抓取对象)如果勾选,Object To Grab(抓取的对象)将被复制到一个新对象并添加到控制器上,而被复制的对象留在场景里。这在两个控制器都需要抓取同样的对象时是需要的,因为单个对象不能被不同的控制器同时抓取。如果被抓取的对象是一个预制就也需要被复制,因为它需要存在于场景中被抓取。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325777177&siteId=291194637