简体   繁体   中英

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. 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:

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. If you just create a new controller in this function, it will always be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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