简体   繁体   English

LeapMotion + Unity:基于手指的角度在3D世界中移动对象

[英]LeapMotion + Unity: Moving an object in a 3D world based on the angle of a finger

I am trying to create an aiming system in Unity, where index finger is being used to control a crosshair. 我正在尝试在Unity中创建一个瞄准系统,该系统中的食指用于控制十字准线。 The game is being played with Oculus and Leap Motion is mounted on to the Oculus headset. 正在使用Oculus玩游戏,并且Leap Motion安装在Oculus耳机上。

I have tried to calculate the position for the crosshair based on the angle of the index finger and draw a ray based on a given distance. 我尝试根据食指的角度计算十字准线的位置,并根据给定的距离绘制射线。 I have also tried calculating the crosshair location just based on the direction of the index finger and given distance like this: 我还尝试仅根据食指的方向和给定距离来计算十字线位置,如下所示:

Vector3 targetPoint = direction * direction;

Here is what I have tried: 这是我尝试过的:

void Start () {
    crossHair = GameObject.Find("Crosshair Component");
    myCamera = GameObject.Find("CenterEyeAnchor");
    target = crossHair.transform;
    controller = new Controller(); 
    indexFinger = new Finger();

    void Update () {

    crossHair.transform.position = myCamera.transform.position;
    frame = controller.Frame();
    List<Hand> handList = new List<Hand>();
    for (int h = 0; h < frame.Hands.Count; h++)
    {
        Hand leapHand = frame.Hands[h];
        handList.Add(leapHand);
    }

    if (handList != null && frame.Hands.Count > 0) {
        indexFinger = frame.Hands[0].Fingers[(int)Finger.FingerType.TYPE_INDEX];
        if (indexFinger.IsExtended)
        {
            Vector3 fingerTipPos = indexFinger.TipPosition.ToUnityScaled();

            Vector3 originAngle = indexFinger.TipPosition.ToUnityScaled();
            Vector3 targetAngle = crossHair.transform.position;
            float distance = 1; 

            Vector3 direction = indexFinger.Direction.ToUnityScaled();
            Ray rayToTest = new Ray(originAngle, targetAngle);
            Vector3 targetPoint = rayToTest.GetPoint(distance);

            //Problem is here. How should these targetPoint values be used to calculate correct position for the crosshair?
            crossHair.transform.position = new Vector3(crossHair.transform.position.x + (targetPoint.x), y,
                                                    crossHair.transform.position.z + (targetPoint.z));
        }


    }
}

} }

With similar calculations as these I have been able to move the crosshair accordingly on horizontal level by modifying x and z values but the y-axis seems to be the biggest problem. 通过类似的计算,我能够通过修改x和z值在水平方向上相应地移动十字准线,但是y轴似乎是最大的问题。 Y-axis seems to always receive too low values. Y轴似乎总是收到太低的值。 The crosshair element is placed as a child element for the CenterEyeAnchor camera object in Unity. 十字线元素被放置为Unity中CenterEyeAnchor相机对象的子元素。

So questions are: Is the way of calculating the target point position right as I have been trying to make it? 因此,问题是:我一直在尝试计算目标点位置的方法正确吗? What kind of calculations would I have to make for the crosshairs position based on the new target point value to make it behave accordingly to the index finger movement? 我必须根据新的目标点值对十字线位置进行什么样的计算,以使其与食指的移动相对应? Scaling factors for the values? 比例因子的值?

Pictures of the project setup and current object positions attached. 项目设置和当前对象位置的图片已附上。

Here is the image for the setup of objects 这是用于设置对象的图像

Here is the image of the crosshair's false position 这是十字准线错误位置的图像

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

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