HTC VIVE开发笔记(四)Interaction System插件手柄交互基础

(1)交互的基础——Interactable组件

给一个物体,或者UI添加Interactbale组件后,标记这个物体是可以和手柄进行交互的。

给Cube添加Interactable组件后,cube即被标记为可以进行交互。当手柄接触到可交互物体时,手柄会高亮,并震动一下。

手柄高亮:

这一部分是在Player组件下的StreamVRObjects-Hand-ControllerHoverHighlight脚本进行控制的。高亮材质:HoverHighlight,使用的是Shader:Valve/VR/Sihouette 这一个,其他需要高亮的也可以用这个shader

手柄震动:

StreamVRObjects-Hand-ControllerHoverHighlight  属性:Fire Haptics OnHightlight来控制震动。

hand.controller.TriggerHapticPulse( 500 );

 (2)物体响应Hand交互事件:如手柄碰到物体时,物体更换材质等;可以根据实际情况来做出想要的操作

对物体进行相应的交互设置:

添加Interactable后可以添加Interactable Hover Events悬停事件,就会出现四个可以选事件在属性面板上。太令人吃惊了,居然不需要写一行代码就可以实现这些操作。

一共有4个事件:

On Hand Hover Begin() : 手柄碰到了物体

On Hand Hover End() : 手柄离开物体

On Attached To Hand() : 响应抓取   

On Detached From Hand() : 响应放下

添加Interactale Button Events: 响应Button事件。也有很多常见的事件。事件处理函数也可以通过脚本来添加。

两种设置响应事件的方法:

静态的配置,在属性面板中进行; 动态的在代码中进行设置;

using Valve.VR.InteractionSystem;
public class InteractWithHandButton : MonoBehaviour {

	// Use this for initialization
	void Start () {
       InteractableButtonEvents ibe = GetComponent<InteractableButtonEvents>();
       ibe.onTriggerDown.AddListener(TriggerDownHandler);
	}
    /// <summary>
    /// 扳机键按下处理函数
    /// </summary>
    public void TriggerDownHandler()
    {
        Debug.Log("Trigger Down");
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
(3) 抓取物体——Throwable组件

设置物体可以被手柄抓取:只需添加Throwable组件即可。太方便了。。

Attachment Flags属性:物体如何吸附在手柄上的一些设置。物体和手柄坐标保持一致

SnapOnAttach:Theobject should snap to the position of the specified attachment point on thehand 物体是吸附到手柄某一个点上的

DetachOthers:Other objects attached to this hand will bedetached.释放其他物体

DetachFromOtherHand:This object will be detached from the otherhand.脱离另一个手柄

ParentToHand:The object will be parented to the hand.物体成为手的子物体

Attachment Point属性:可以指定一个gameobject使抓取到的物体吸附到这个gameobject上

Catch Speed Threshold: 手柄抓取的响应时间

Restore Original Parent:是否在抓取放下后保持原来的父子层级关系

Attach Ease In:是否有缓动,可以设置缓动类型和缓动时间

On Pick Up():拿起的响应事件

OnDetach From Hand() 放下之后响应的事件

(4)使用脚本让Unity强制添加某些必要的组件

写脚本时,可以告诉Unity,这个脚本还需要这些组件,强制添加组件,防止不必要的错误,例如引用不到,编译错误等。平时写脚本时也可以这样写。

[RequireComponent( typeof( Interactable ) )]
	[RequireComponent( typeof( Rigidbody ) )]
	[RequireComponent( typeof( VelocityEstimator ) )]

(5)碰撞时真实的物理属性

比如一些具有弹性的物体,掉在地上,可以通过在Collider中设置Material来进行处理。

猜你喜欢

转载自blog.csdn.net/u011310341/article/details/78459291