leapmotion开发(3)实例操作

1.在unity中新建一个项目。导入

2.将leaphandcontroller拖到hierarchy中。

3.创建一个空物体。hierarchy中右键选择create empty,重命名为HandModels。

然后把assets/prefabs/HandModelsNonhuman中的capsulehand_R,capsulehand_L拖到HandModels下。

再把assets/prefabs/HandModelsPhysical中的RigidRoundHand_R,RigidRoundHand_L拖到HandModels下。

4.然后点击leapmotioncontroller,在inspector视窗中添加hand pool插件。size设为2。(两个元素,一个方图形手,一个放物理手。)做如下图设置。


这里left model和right model直接将hierarchy中的相应模型拖进去即可。下面两个参数记得打上勾。将handModels拖到Models Parent中。

5.运行之后就可以在窗口中看到两只手。如果看不到可以调节一下相机的位置。

这样就可以在unity中看到自己的双手了。如果不想自己调节,也可以直接使用核心包中的预设场景。在leapmotion/scenes中选择leap_hands_demo_desktop即可。(因为我只用了leapmotion并没有使用头盔设备选择desktop就可以了)如果有疑惑可以看我翻译的leapmotion文档(3)

接下来想利用leapmotion监测手来移动场景中的物体。

1.先建立一个cube。

2.创建一个c#脚本。

using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;

public class NewBehaviourScript : MonoBehaviour {
	LeapProvider provider;
	// Use this for initialization
	void Start () {
		provider = FindObjectOfType<LeapProvider> ()as LeapProvider;
	}
	
	// Update is called once per frame
	void Update () {
		Frame frame = provider.CurrentFrame;
		foreach (Hand hand in frame.Hands) 
		{
			if (hand.IsLeft)
			{
				transform.position = hand.PalmPosition.ToVector3 () + hand.PalmNormal.ToVector3 () * (transform.localScale.y * .5f + .02f);
				//transform.rotation = hand.Basis.rotation();
			}
		}
	}
}
3.然后将脚本拖到cube物体上。运行即可。

在这一步中,我调了很久。一开始把代码复制过去,会报错。显示未能解析主引用LeapCSharp.NET3.5。这导致代码里所有的finger类型,hand类型都找不到。解决方法是右键点击项目选择options。然后在build-general中将target framework改成mono/.NET.4.0版本。

如果还有错可以试下重新下载unity,我用的版本是unity5.4.2。

然后注意一定要导入核心包,因为代码里using Leap.Unity就是引用了核心包里一些函数。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章