繁体   English   中英

参数超出范围(Unity3d LeapMotion)

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

所以我正在使用此代码:

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手翻译。 但是,当我运行此脚本时,出现异常``参数超出范围''并且程序无法运行。我试图将项目添加到列表中,但未能这样做。

您正试图访问手列表,即使该列表可能为空

尝试这个:

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

        }
    }
}

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对象添加到控制面板中。 如果仅在此函数中创建一个新的控制器,它将始终为空。

暂无
暂无

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

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