简体   繁体   English

参数超出范围(Unity3d LeapMotion)

[英]Argument is out of range (Unity3d LeapMotion)

So I am working with this code: 所以我正在使用此代码:

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));

        }
    }
}

This is used for LeapMotion hands translation in Unity. 这用于Unity中的LeapMotion手翻译。 But when I run this script I get the exception'Argument is out of Range' and the program does not work .I tried to add items into the list but haven't been able to do so . 但是,当我运行此脚本时,出现异常``参数超出范围''并且程序无法运行。我试图将项目添加到列表中,但未能这样做。

You're tring to access the Hands List even when it could be empty 您正试图访问手列表,即使该列表可能为空

try this: 尝试这个:

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));

        }
    }
}

The hands list is likely empty: 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

You need to add some Hand objects to the contoller at some point. 您需要在某个时候将一些Hand对象添加到控制面板中。 If you just create a new controller in this function, it will always be empty. 如果仅在此函数中创建一个新的控制器,它将始终为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM