使用Unity Framework进行Leap运动的游标

时间:2018-06-17 05:07:59

标签: c# unity3d game-physics leap-motion

我是Unity和Leap Motion的新手。 但是,我试图与我在游戏中写入的游戏对象和脚本进行交互。我的游戏使用Unity Leap Interaction Engine,交互行为和其他Leap运动脚本。我终于让gameObjects抓住并将物体再次放回地面。但我不希望游戏对象在没有我的游戏规则的情况下重新回到原地。我想抓住游戏对象并将其放在我在游戏中编写的规则中。但由于交互行为脚本,它没有发生。我的代码我已写入下面给出的鼠标光标。如果您知道如何操作,请告诉我如何将此代码转换为Leap Motion c#脚本?谢谢你提前。

private void Update()
{
    UpdateCursorOver();
    //if it is my turn 
    {
        int x = (int)mouseOver.x;
        int y = (int)mouseOver.y;

       if (selected_Piece != null)
        {
            UpdatePiDrag(selected_Piece);
        }

        if (Input.GetMouseButtonDown(0))
        {
            Select_Piece(x, y);
        }

        if (Input.GetMouseButtonUp(0))
        {
            TryMove((int)start_Drag.x, (int)start_Drag.y, x, y);

        }
    }
    Update_Selection();

    DrawBoard();
}



 private void UpdateCursorOver()
    {
        // if its my turn 
        if (!Camera.main)
        {
            Debug.Log("Unable to find main camera");
            return;
        }
        RaycastHit hit;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("BoardPlane")))
        {
            selectionX = (int)hit.point.x;
            selectionY = (int)hit.point.z;

            mouseOver.x = selectionX;
            mouseOver.y = selectionY;
        }
        else
        {
            mouseOver.x = -1;
            mouseOver.y = -1;
        }
    }

但是,我创建了一个新脚本以便更改它。我这样做只是为了实验。但它仍然没有用......

using System.Collections;
using System.Runtime.InteropServices;

using UnityEngine;
using Leap;

public class PieceTouch : MonoBehaviour {

    Leap.Controller controller;

    public void EnableCursorLeap()
    {
        controller = new Controller();
        Frame frame = controller.Frame();
        var coordinate = GetNormalizedXAndY(frame);


        if ((int)frame.Hands.Fingers[0].TipVelocity.Magnitude <= 25) return;
        SetCursorPos((int)coordinate.X, (int)coordinate.Y);
        if (frame.Fingers.Count != 2) return;
        mouse_event(0x0002 | 0x0004, 0, (int)coordinate.X, (int)coordinate.Y, 0);
    }

    [DllImport("user32.dll")]
    private static extern bool SetCursorPos(int x, int y);

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    static Point GetNormalizedXAndY(Frame frame)
    {
        var interactionBox = frame.InteractionBox;
        var vector = interactionBox.NormalizePoint(frame.Fingers.Frontmost.StabilizedTipPosition);

        var width = SystemInformation.VirtualScreen.Width;
        var height = SystemInformation.VirtualScreen.Height;
        var x = (vector.x * width);
        var y = height - (vector.y * height);

        return new Point() { X = (int)x, Y = (int)y };
    }


    // Use this for initialization
    void Start () {


    }

    // Update is called once per frame
    void Update () {



    }
}

0 个答案:

没有答案