所以我正在使用这段代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
public class iliketomoveit : MonoBehaviour {
Controller controller;
float HandPalmPitch;
float HandPalmYam;
float HandPalmRoll;
float HandWristRot;
void Start () {
}
// Update is called once per frame
void Update () {
controller = new Controller();
Frame frame = controller.Frame();
List<Hand> hands = frame.Hands;
if (frame.Hands.Count > 0)
{
Hand fristHand = hands[0];
}
HandPalmPitch = hands[0].PalmNormal.Pitch;
HandPalmRoll = hands[0].PalmNormal.Roll;
HandPalmYam = hands[0].PalmNormal.Yaw;
HandWristRot = hands[0].WristPosition.Pitch;
Debug.Log("Pitch :" + HandPalmPitch);
Debug.Log("Roll :" + HandPalmRoll);
Debug.Log("Yam :" + HandPalmYam);
if (HandPalmYam > -2f && HandPalmYam < 3.5f)
{
transform.Translate ( new Vector3(0, 0,1 * Time.deltaTime));
}else if (HandPalmYam < -2.2f)
{
transform.Translate ( new Vector3(0, 0, -1 * Time.deltaTime));
}
}
}
这用于Unity中的LeapMotion指针翻译。但是当我运行这个脚本时,我得到异常'争论超出范围'并且程序不起作用。我试图将项目添加到列表中但是却无法这样做。
答案 0 :(得分:1)
即使它可能是空的,你也可以访问动手列表
试试这个:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
public class iliketomoveit : MonoBehaviour {
Controller controller;
float HandPalmPitch;
float HandPalmYam;
float HandPalmRoll;
float HandWristRot;
void Start () {
}
// Update is called once per frame
void Update () {
controller = new Controller();
Frame frame = controller.Frame();
List<Hand> hands = frame.Hands;
if (frame.Hands.Count == 0) return;
Hand fristHand = hands[0];
HandPalmPitch = hands[0].PalmNormal.Pitch;
HandPalmRoll = hands[0].PalmNormal.Roll;
HandPalmYam = hands[0].PalmNormal.Yaw;
HandWristRot = hands[0].WristPosition.Pitch;
Debug.Log("Pitch :" + HandPalmPitch);
Debug.Log("Roll :" + HandPalmRoll);
Debug.Log("Yam :" + HandPalmYam);
if (HandPalmYam > -2f && HandPalmYam < 3.5f)
{
transform.Translate ( new Vector3(0, 0,1 * Time.deltaTime));
}else if (HandPalmYam < -2.2f)
{
transform.Translate ( new Vector3(0, 0, -1 * Time.deltaTime));
}
}
}
答案 1 :(得分:1)
hands
列表可能为空:
controller = new Controller(); // Controller is new, we can assume it is empty
Frame frame = controller.Frame(); // Frame come from new controller, we can assume it is empty
List<Hand> hands = frame.Hands; // Hands come from empty Frame, we can assume it is empty
if (frame.Hands.Count > 0) // If hands is empty, this is skipped
{
Hand fristHand = hands[0];
}
HandPalmPitch = hands[0].PalmNormal.Pitch; // You try to modify the first element of Hands, but it doesn't exist
您需要在某个时刻向控制器添加一些Hand
个对象。如果您只是在此函数中创建一个新控制器,它将始终为空。