C# 参数超出范围(Unity3d LeapMotion)

C# 参数超出范围(Unity3d LeapMotion),c#,unity3d,leap-motion,C#,Unity3d,Leap Motion,因此,我正在使用以下代码: using System.Collections; using System.Collections.Generic; using UnityEngine; using Leap; using Leap.Unity; public class iliketomoveit : MonoBehaviour { Controller controller; float HandPalmPitch; float HandPalmYam; fl

因此,我正在使用以下代码:

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

        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用Leap;
运用跳跃、统一;
公共类信息技术:单一行为{
控制器;
浮掌;
花椰菜;
浮动手卷;
浮式手快步;
无效开始(){
}
//每帧调用一次更新
无效更新(){
控制器=新控制器();
Frame=controller.Frame();
列表指针=frame.hands;
如果(frame.Hands.Count>0)
{
手最前面=手[0];
}
HandPalmPitch=手[0]。PalmNormal.Pitch;
HandPalmRoll=手[0].PalmNormal.Roll;
HandPalmYam=hands[0].PalmNormal.Yaw;
HandWristRot=手[0]。腕部位置。节距;
调试日志(“音高:+手掌音高);
Debug.Log(“Roll:+handpoll”);
Log(“Yam:+handpalmym”);
如果(手掌大于-2f和手掌小于3.5f)
{
transform.Translate(新向量3(0,0,1*Time.deltaTime));
}否则如果(手掌<-2.2f)
{
transform.Translate(新向量3(0,0,-1*Time.deltaTime));
}
}
}

这用于统一的跳跃运动手平移。但是当我运行这个脚本时,我得到了一个异常“参数超出范围”,程序无法运行。我试图将项目添加到列表中,但未能这样做

您正在尝试访问手动列表,即使它可能是空的

试试这个:

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

        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用Leap;
运用跳跃、统一;
公共类信息技术:单一行为{
控制器;
浮掌;
花椰菜;
浮动手卷;
浮式手快步;
无效开始(){
}
//每帧调用一次更新
无效更新(){
控制器=新控制器();
Frame=controller.Frame();
列表指针=frame.hands;
如果(frame.Hands.Count==0)返回;
手最前面=手[0];
HandPalmPitch=手[0]。PalmNormal.Pitch;
HandPalmRoll=手[0].PalmNormal.Roll;
HandPalmYam=hands[0].PalmNormal.Yaw;
HandWristRot=手[0]。腕部位置。节距;
调试日志(“音高:+手掌音高);
Debug.Log(“Roll:+handpoll”);
Log(“Yam:+handpalmym”);
如果(手掌大于-2f和手掌小于3.5f)
{
transform.Translate(新向量3(0,0,1*Time.deltaTime));
}否则如果(手掌<-2.2f)
{
transform.Translate(新向量3(0,0,-1*Time.deltaTime));
}
}
}

手的
列表可能是空的:

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
controller=newcontroller();//控制器是新的,我们可以假设它是空的
Frame Frame=controller.Frame();//帧来自新的控制器,我们可以假设它是空的
列表指针=frame.hands;//手来自空框,我们可以假设它是空的
if(frame.Hands.Count>0)//如果Hands为空,则跳过此操作
{
手最前面=手[0];
}
HandPalmPitch=手[0]。PalmNormal.Pitch;//您尝试修改手的第一个元素,但它不存在

您需要在某个点向控制器添加一些
Hand
对象。如果您只是在该函数中创建一个新控制器,它将始终为空。

如果您提供有关错误的更多详细信息,例如出现错误的行,它将尝试应用此修复程序,这将总是更容易提供帮助。。使用此修复程序,我将能够导航leapMotion手控制器?我这样做了,错误得到解决,但手似乎只是向前移动,似乎没有控制。有什么想法吗?