JK Romania
JK Romania

Reputation: 23

Cursor in Leap motion with Unity Framework

I am New to Unity and Leap Motion. However, I was trying to interact with my game objects and scripts that I have written into the game. My game is used Unity Leap Interaction Engine, Interaction Behavior and other Leap motion scripts.

I finally got the gameObjects grabbing and putting the objects back again into the ground. But I don't want the game objects to put back again the ground without my game rules. I want to grab the game object and place it on the ground within rules that I have written in the game. But it was not happening because of Interaction Behavior scripts.

My code I have written into the mouse cursor given below. How can I convert this code to Leap Motion C# scripts?

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

However, I created a new Script in order to change this. I do this just to experiment. But still it didn't work.

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 () {
    }
}

Upvotes: 2

Views: 560

Answers (0)

Related Questions