Entry 5: How to Integrate Leap Motion in Unity [Coding the Leap Motion = Steering]

Unity ver 5.6

After been weeks of researching and study on how Leap Motion to work with Unity. I've used different approaches on doing it.

These are the tutorials I've used:

YouTube. (2018). Leap Motion Orion Tutorial in Unity: Installation and Setup. [online] Available at: https://www.youtube.com/watch?v=Wh3YK2OcI4M [Accessed 30 Jan. 2018].

A tutorial for Leap Motion integration with Unity for beginners.

YouTube. (2018). How to use leap motion in unity. [online] Available at: https://www.youtube.com/watch?v=X__oClQTob0 [Accessed 30 Jan. 2018].

A quick tutorial for Leap Motion Assets into Unity.


Firstly, this what I do the controls:

Step 1 : Download Leap Motion SDK (Orion Beta) in Leap Motion website [https://developer.leapmotion.com/unity/#116]

Step 2: Get Unity Core Assets 4.3.4 (Leap Motion) on the same site for Unity. [https://developer.leapmotion.com/unity/#116]

Step 3: Open Unity and import the Unity Core Asset (Leap Motion).

Step 4: Open the example

Step 5: Explore the codes used

Step 6: Since this game require Leap Motion x-axis to control, I'll need to find data from the coding from the examples that use x-axis.

Step 7: Find "Capsule Hand"


Step 8: I'm trying to get data FROM the palm therefore, I'll need the data called "palmPosition".


Step 9: If I "Debug.Log(palmPosition)", I'll get X,Y,Z axis my hand. As I mentioned earlier, we need only x-axis. So I've "Debug.Log(palmPosition.x)" instead.

Step 10: Run the game and the value of x-axis from my palm should print on the console.

Step 11: With that data I can do this. I've created a variable that identify the "palmPosition.X"as "handposX". I've also made a public float for the variable. Since the data are really small, I've "amp-ed" up the value by multiplying with 1000.



Step 12: The stuff I've did is in "not-so-proper" place. Therefore, I'd create a new C# script that is called "leapXData". Technically it's used to control the car. [Big Thanks to Jeremy Lim for helping me on this coding]


Step 13: Get the "handposX" value from the Capsule Hand and link it to leapXData. In leapXData, I've added a public gameObject that get's the handPosX data from it, add a public rigidbody which it'll be my car. 

Here's the code of the completed leapXData:-








using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
using Leap.Unity.Attributes;

public class leapXData : MonoBehaviour {
 //   private Hand _hand;
    public GameObject handL;
    public GameObject handR;

    public CapsuleHand capsuleHandL;
    public CapsuleHand capsuleHandR;
    public Rigidbody rb;
    public float hannyPos;
    public float centerPos;
    [SerializeField] public float smooth;

    Vector3 position;


    // Use this for initialization
    void Start () {

        rb.velocity = Vector3.ClampMagnitude(rb.velocity = Vector3.right, 10);
        capsuleHandL = handL.GetComponent<CapsuleHand>(); rb = GetComponent<Rigidbody>();

        capsuleHandR = handR.GetComponent<CapsuleHand>(); rb = GetComponent<Rigidbody>();

        position = transform.position;
        centerPos = transform.position.x;
        
    }
// Update is called once per frame
void Update ()
    {

        if (handR.activeSelf && !handL.activeSelf)
        {
           
            hannyPos = capsuleHandR.handposX * 4;
            //print(hannyPos);
            //print(handL);

         }
        else if (handL.activeSelf && !handR.activeSelf)
        {
            hannyPos = capsuleHandL.handposX * 4;
            //print(hannyPos);
            //print(handL);
        }
        else
        {
            hannyPos = Mathf.Lerp(transform.position.x, centerPos, smooth* Time.deltaTime);
        }


        rb.transform.position = new Vector3(hannyPos, rb.transform.position.y, rb.transform.position.z);
        

    }












WHENEVER you're creating a script that include Leap Motion functions, add this.



Step 14: With leapXData, you can link the Capsule Hands(from Leap Motion Assets) and link your player, in this case is a car.




Step 15: The car should be steered by the controls from Leap Motion.

This is my Hierarchy




There's this bug I've not fix is whenever I control my car with Leap Motion (in x-axis), my car will just PHASE THROUGH the side walls of the "obstacles" I've made. The "not-so-great" solution can be found in my later post.

Before this Leap Motion coding, I've used GameWAVE to control the game but it seems it's not working because it doesn't work when its window is not active
[ https://gallery.leapmotion.com/gamewave/ ]

If there's any questions or doubts, you can email me : edwin.siew139@gmail.com.

Comments

Popular posts from this blog

Entry 9: Post-Processing in Unity